我的 GUI 中有一些嵌套布局。 不要问我为什么这样做或者为什么我不使用网格布局......我的问题是别的。
我的布局看起来像 THIS
在我向这 4 个相对布局中的任何一个添加任何 View (比如说一个按钮)后,它会更改为 THIS
我将相对布局的权重设置为 0dp。这样,当我向布局添加另一个 View 时,宽度不会改变。但不知道高度怎么办。如果我也将其设置为 0dp,它就会消失。
这是我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rising_sun_blue"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#333111"
android:weightSum="2"
android:layout_alignParentBottom="true"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#123132"
android:weightSum="2"
android:layout_alignParentTop="true">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
所以,我的问题是,在我添加一堆新 View 后,是否有任何代码可以“粘合”我的布局并使它们保持与第一个屏幕截图完全相同!?
最佳答案
给两个嵌套的LinearLayouts android:layout_height="match_parent"而不是wrap_content。
关于android - LinearLayout 高度随添加新 View 而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22956932/