问题描述
我有一个CardView for RecyclerView项目,其中包含一个ImageView和LinearLayout(actionBar)
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/communityImageCv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
app:cardCornerRadius="30dp"
android:elevation="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/communityImageCl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatimageView
android:id="@+id/communityImageIv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/actionBar"
tools:src="@drawable/person_splash_2" />
<LinearLayout
android:id="@+id/actionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:background="@color/white"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:visibility="gone"
android:weightSum="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:visibility="visible">
<LinearLayout
android:id="@+id/loveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="@+id/lovedIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:src="@drawable/loved_heart_empty">
</ImageView>
<TextView
android:id="@+id/lovedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/radikal"
android:textColor="@color/black"
android:textSize="10sp"
tools:text="@string/love_this">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible"
>
<ImageView
android:id="@+id/shareIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:src="@drawable/share_arrow_empty">
</ImageView>
<TextView
android:id="@+id/sharedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/radikal"
android:textColor="@color/black"
android:textSize="10sp"
tools:text="@string/shared_this">
</TextView>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
LinearLayout被编程为根据模型的属性出现和消失。然后,根据另一个属性以编程方式更改ImageView的高度(如果相关,我仅包括此部分):
fun bind(model: CommunityOffer) {
if (model.isLikeable || model.isShareable) {
actionBar.visibility = View.VISIBLE
} else{
actionBar.visibility = GONE
}
if (!model.mediaAspectRatio.isNullOrEmpty()) {
val aspectRatioSplit = model.mediaAspectRatio.split(":")
widthRatio = Integer.parseInt(aspectRatioSplit[0])
heightRatio = Integer.parseInt(aspectRatioSplit[1])
communityImageIv.scaleType = ImageView.ScaleType.FIT_XY
height = ((width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
communityImageIv.layoutParams.height = height
communityImageCl.layoutParams.height = height
Glide.with(itemView.context).load(model.coverUrl).apply(RequestOptions().override(width,height).skipMemoryCache(true)).listener(requestListener).into(communityImageIv)
} else {
communityImageIv.scaleType = ImageView.ScaleType.CENTER_CROP
widthRatio = 25
heightRatio = 21
height = ((width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
communityImageIv.layoutParams.height = height
communityImageCl.layoutParams.height = height
Interactors.glide.loadImage(model.coverUrl,communityImageIv,requestListener)
}
CardView并没有增加其两个子视图的大小,而是根据actionBar的大小按比例向上移动了ImageView,从而切断了ImageView的顶部:
没有actionBar:
使用actionBar:
这看起来应该如何显示在.xml文件的显示面板中,而不是在运行时显示。如何使CardView在运行时适当地包装其子代?
解决方法
我认为您应该尝试将ConstraintLayout的线性布局作为cardView的子布局,而不是在linearLayout中设置所有子窗口小部件,希望如此。
像这样
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/communityImageCv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
app:cardCornerRadius="30dp"
android:elevation="16dp">
<LinearLayout
android:id="@+id/communityImageCl"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
//set here your all child widgets
</LinearLayout>
</androidx.cardview.widget.CardView>