在我的应用程序 XML 布局中,我有一个 Relativelayout,其中有两个线性布局。现在我正在为这两种布局制作动画。但是因为 thera 看起来高度不同,所以我遇到了一些 View 问题。
所以我想使两个布局的高度相同。下面是我的 XML 文件。
<!-- ============================================================= -->
<!-- BOTTLE / PEN LAYOUT -->
<!-- ============================================================= -->
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<!-- ============================================================= -->
<!-- PEN LAYOUT -->
<!-- ============================================================= -->
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/pen_layout">
<TextView
android:text="EF"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:textSize="10sp"/>
<ImageView
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ink_pen"/>
<TextView
android:text="F"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:textSize="10sp"/>
<ImageView
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ink_pen"/>
<TextView
android:text="BOLD"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:textSize="10sp"/>
<ImageView
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ink_pen"/>
<TextView
android:text="ITALIC"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:layout_marginTop="2dp"
android:textSize="10sp"/>
<ImageView
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ink_pen"/>
</LinearLayout>
<!-- ============================================================= -->
<!-- BOTTLE LAYOUT -->
<!-- ============================================================= -->
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="4"
android:id="@+id/bottle_layout">
<!-- First Row Bottle -->
<LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="fill_parent" android:weightSum="2">
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/purple_bottle"/>
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/red_bottle"/>
</LinearLayout>
<!-- Second Row Bottle -->
<LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="fill_parent" android:weightSum="2">
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/gray_bottle"/>
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/green_bottle"/>
</LinearLayout>
<!-- Third Row Bottle -->
<LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="fill_parent" android:weightSum="2">
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/orange_bottle"/>
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/blue_bottle"/>
</LinearLayout>
<!-- Forth Row Bottle -->
<LinearLayout android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="fill_parent" android:weightSum="2">
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/black_bottle"/>
<ImageView android:layout_height="wrap_content" android:layout_weight="1"
android:layout_width="wrap_content" android:src="@drawable/white_bottle"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
最佳答案
删除包含的 RelativeLayout(因为它是您的根 LinearLayout 的唯一子级,您并不严格需要它)或将其更改为 LinearLayout。
然后对于 Pen 布局和 bottle 布局,添加属性 android:layout_weight 并将它们都设置为 1。
确保两个布局的高度都设置为 MATCH_PARENT
现在你的两个布局应该在它们之间平均共享空间,从而使它们具有相同的高度。
为了在 Pen 和 Bottle 布局的子级之间平均共享空间,我建议您将每个子级的高度设置为 WRAP_CONTENT,并将每个子级的 layout_weight 设置为 1。
关于android - 如何使两个不同的布局具有相同的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295556/