如何动态处理两种布局的android



父布局中有两个垂直布局。第一个布局有图像,第二个布局有一些相对数据。第二布局高度可以根据数据增加。如何管理与第二布局相对应的第一布局高度。我想根据第二个布局的高度设置图像背景颜色。

<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="@dimen/sp_5"
app:cardElevation="@dimen/margin_5dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/parentContainer"
android:layout_width="match_parent"
android:layout_height="200sp"
android:padding="@dimen/sp_5"
android:layout_marginTop="@dimen/sp_10"
android:layout_marginBottom="@dimen/sp_10"
android:background="@drawable/top_rounded_background">
<RelativeLayout
android:id="@+id/store_cat_img_container"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="120dp"
android:layout_centerVertical="true"
app:cardCornerRadius="@dimen/sp_10"
app:cardElevation="@dimen/sp_10">
<ImageView
android:id="@+id/productImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@mipmap/images" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/store_cat_img_container"
android:paddingLeft="@dimen/sp_10">
<LinearLayout
android:id="@+id/productNameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/productName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="product Name"
android:textColor="@color/black"
android:textSize="@dimen/dimen_13dot7sp"
android:textStyle="bold"
android:layout_weight="1.9"/>
<TextView
android:id="@+id/productOffer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="@dimen/sp_10"
android:layout_toRightOf="@+id/productName"
android:layout_weight="1.1"
android:text="offer"
android:visibility="gone"
android:textSize="@dimen/sp_12" />
</LinearLayout>
<TextView
android:id="@+id/productDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/productNameLayout"
android:text="product description.  product is very nice, good quality product. "
android:textColor="@color/darkGrey"
android:textSize="@dimen/sp_10" />
<LinearLayout
android:id="@+id/priceQuantLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/productDescription"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/productStrikePrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/productDescription"
android:layout_marginRight="@dimen/sp_5"
android:layout_toLeftOf="@id/productPrice"
android:layout_weight="0.2"
android:textColor="@color/darkGrey"
android:textSize="@dimen/dimen_15sp"
android:visibility="gone" />
<TextView
android:id="@+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/productDescription"
android:layout_weight="0.2"
android:text="u20B9"
android:textColor="@color/dustyred"
android:textSize="@dimen/dimen_15sp" />
<TextView
android:id="@+id/productQnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/productDescription"
android:layout_toRightOf="@+id/productPrice"
android:text="1 kg"
android:textColor="@color/darkGrey"
android:textSize="@dimen/sp_12" />
</LinearLayout>
<LinearLayout
android:id="@+id/addToCartEditableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/priceQuantLayout"
android:backgroundTint="@color/lightGrey"
android:gravity="center">
<ImageButton
android:id="@+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/semiTransparent"
android:src="@mipmap/icons_minus" />
<EditText
android:id="@+id/inputCountText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/margin_30dp"
android:layout_marginRight="@dimen/margin_30dp"
android:backgroundTint="@color/white_traspartent"
android:inputType="number"
android:text="1"
android:textColor="@color/sea_green"
android:textSize="@dimen/sp_12" />
<ImageButton
android:id="@+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/semiTransparent"
android:src="@mipmap/icons_add" />
</LinearLayout>
<LinearLayout
android:id="@+id/addToCartLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/sp_5"
android:layout_below="@+id/addToCartEditableLayout">
<Button
android:id="@+id/addToCart"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:backgroundTint="@color/dark_orange"
android:fitsSystemWindows="true"
android:text="Add to cart"
android:textAllCaps="false"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>

我会使用ConstraintLayout来构建整体布局。这是处理复杂布局的最佳和推荐的方式。如果你需要根据屏幕宽度和高度来控制它,它有百分比支持。此外,您还可以使用指南。