Android LinearLayout and weights issue



我在Android中使用LinearLayout和权重时遇到问题。我希望水平线性布局包含 2 个垂直线性布局,由具有 9 个补丁背景的单个视图分隔,作为 2 个垂直线性布局之间的分隔符。

像这样:(外框是外部线性布局,中间的双线是我的 9 个补丁分隔符。

----------------------------
|    one    ||    three    |
|    two    ||    four     |
----------------------------

不断发生的是第一个内部 LinearLayout 显示器以最小的宽度显示其内容(好像它的宽度是 wrap_content ),然后其余空间被分隔符视图占用,拉伸以填充外部 LinearLayout 的其余部分。第二个内部线性布局根本不显示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_weight="1" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two" />
    </LinearLayout>
    <View
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/divider_vertical"
        android:layout_weight="0" />
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_weight="1" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="four" />
    </LinearLayout>
</LinearLayout>

我在这里做错了什么?我一辈子都想不通为什么中间的视图占据了所有的空间,而没有留下第二个内部线性布局的空间。

如果我为 9 个补丁视图指定特定的 px 或 dp 宽度,我可以让它工作,但我真的希望它无需指定此宽度即可工作。这样,如果我决定更改我的 9 个补丁可绘制对象,我就不必手动更新宽度。

尝试此布局

更新的布局

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="one" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two" />
</LinearLayout>
<View
    android:layout_width="1dp"
    android:layout_height="88dp"
    android:background="@android:color/darker_gray" />
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="three" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="four" />
</LinearLayout>

Updtae 在这里尝试

<View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@drawable/divider_vertical"
 />
  • 从父线性布局中删除weightSum="2"
  • 从 9 修补程序视图中移除android:layout_weight="0"

您的 9 个补丁有多大? 9 个补丁的可拉伸区域是不可收缩的 - 资产文件的宽度将决定它尝试在 LinearLayout 中占用的最小空间。

设置布局权重如下

请记住父项的权重总和,默认情况下它设置为子项的layout_weight和

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_weight="3" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two" />
    </LinearLayout>
    <View
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/divider_vertical"
        android:layout_weight="1" />
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_weight="3" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="four" />
    </LinearLayout>
</LinearLayout>

当然,您可以尝试使用Weigths的值

最新更新