如何在Android布局中让两个MaterialCardView高度为wrap_content自适应
问题描述
我在线性布局或相对布局中有两个MaterialCardView,对父布局类型无要求,仅需要实现两个MaterialCardView的高度都为wrap_content,可根据内部内容自适应调整高度。
当前第一个MaterialCardView内部嵌套FrameLayout,包含1个ImageView和1个通过include引入的、用于展示图片悬浮按钮的布局;第二个MaterialCardView同样嵌套FrameLayout,包含若干TextView和1个宽高均为wrap_content的ImageView(用作按钮)。
当前我设置了weightSum属性,两个MaterialCardView高度均为match_parent,但直接将高度修改为wrap_content无法生效。
注意事项
1:该布局是垂直RecyclerView的一部分item布局
2:ImageView上方的Sort和CategoryTextView不属于当前布局
3:如需更多代码参考可告知,我会更新问题内容
代码部分
更新说明
已移除按钮布局XML代码,避免问题过于冗长混乱
按照评论建议改用ConstraintLayout后,两个卡片已能根据内部内容适配高度,但布局出现错乱。期望的最终效果为固定高度样式(仅作效果参考),改用ConstraintLayout并设置wrap_content高度后布局出现错位。
更新后代码(post_item_container_home.xml,已设置wrap_content和ConstraintLayout)
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@color/black" android:orientation="vertical"> <com.google.android.material.card.MaterialCardView android:id="@+id/Card_View" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginEnd="5dp" android:elevation="6dp" app:cardBackgroundColor="@color/black" app:shapeAppearanceOverlay="@style/RoundedCornerHome" tools:ignore="MissingConstraints"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.imageview.ShapeableImageView android:id="@+id/imagePostHome" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:adjustViewBounds="true" android:contentDescription="@string/todo" android:tag="centerCrop" app:shapeAppearanceOverlay="@style/RoundedCornerHome" /> <include android:id="@+id/imagepost_buttons" layout="@layout/imagepost_buttons" /> </FrameLayout> </com.google.android.material.card.MaterialCardView> <com.google.android.material.card.MaterialCardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginTop="10dp" android:layout_marginEnd="5dp" android:layout_marginBottom="10dp" android:elevation="6dp" app:cardBackgroundColor="@color/dark_grey" app:layout_constraintBottom_toBottomOf="@+id/Card_View" app:shapeAppearanceOverlay="@style/RoundedCornerHome" tools:ignore="MissingConstraints"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|right" android:layout_marginTop="5dp" android:layout_marginEnd="25dp" android:contentDescription="@string/todo" android:src="@drawable/ic_baseline_attach_money_24" tools:ignore="RtlHardcoded" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:fontFamily="@font/roboto" android:text="Ian Somerhalder" android:textColor="@color/white" android:textSize="20sp" android:textStyle="bold" tools:ignore="SmallSp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_marginLeft="10dp" android:fontFamily="@font/roboto" android:text=".............." android:textColor="@color/white" android:textSize="17sp" tools:ignore="RtlHardcoded" /> </FrameLayout> </com.google.android.material.card.MaterialCardView> </androidx.constraintlayout.widget.ConstraintLayout>
旧版本/原始代码(post_item_container_home.xml)
<LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="@color/black" android:orientation="vertical" android:weightSum="5"> <com.google.android.material.card.MaterialCardView android:id="@+id/Card_View" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginStart="5dp" android:layout_marginEnd="5dp" android:layout_weight="4" android:elevation="6dp" app:cardBackgroundColor="@color/black" app:shapeAppearanceOverlay="@style/RoundedCornerHome"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.imageview.ShapeableImageView android:id="@+id/imagePostHome" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:adjustViewBounds="true" android:contentDescription="@string/todo" android:tag="centerCrop" app:shapeAppearanceOverlay="@style/RoundedCornerHome" /> <include android:id="@+id/imagepost_buttons" layout="@layout/imagepost_buttons" /> </FrameLayout> </com.google.android.material.card.MaterialCardView> <com.google.android.material.card.MaterialCardView android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginStart="5dp" android:layout_marginTop="10dp" android:layout_marginEnd="5dp" android:layout_marginBottom="10dp" android:layout_weight="1" android:elevation="6dp" app:cardBackgroundColor="@color/dark_grey" app:shapeAppearanceOverlay="@style/RoundedCornerHome"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|right" android:layout_marginTop="5dp" android:layout_marginEnd="25dp" android:contentDescription="@string/todo" android:src="@drawable/ic_baseline_attach_money_24" tools:ignore="RtlHardcoded" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:fontFamily="@font/roboto" android:text="Ian Somerhalder" android:textColor="@color/white" android:textSize="20sp" android:textStyle="bold" tools:ignore="SmallSp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_marginLeft="10dp" android:fontFamily="@font/roboto" android:text=".............." android:textColor="@color/white" android:textSize="17sp" tools:ignore="RtlHardcoded" /> </FrameLayout> </com.google.android.material.card.MaterialCardView> </LinearLayout>
内容的提问来源于stack exchange,提问作者Vasant Raval
相关产品推荐
相关产品推荐

