【问题标题】:Layout weight not working properly for nested linear layouts布局权重不适用于嵌套线性布局
【发布时间】:2017-06-18 12:23:04
【问题描述】:

我想要有一个像attached这样的活动

我想要三行,每行有三个水平文本视图。这是我建议的布局

<LinearLayout
vertical>
    <LinearLayout
    weight=0.3
    horizontal>

        <TextView
        weight=0.5
        width=match_parent
        width=match_parent
        />

        <TextView
        weigth=0.25
        width=match_parent
        width=match_parent
        />

        <TextView
        weigth=0.25
        width=match_parent
        width=match_parent
        />

    </LinearLayout>

    <SIMILAR LINEAR LAYOUTS HERE>

</LinearLayout

这样,具有 50% 的 TextView 将堆叠到最左侧,完全没有宽度。我不确定会出现什么问题。当我将所有三个相同重量的视图放在一起时,它们可以很好地水平堆叠。

【问题讨论】:

    标签: android android-layout android-linearlayout android-layout-weight


    【解决方案1】:

    如果你同时给layout_weightlayout_width(水平方向),它的工作原理如下

    宽度之和 = layout_weight + layout_width

    Vertical方向也有同样的逻辑。

    将您的布局更改为此并重试:

    <LinearLayout
    vertical>
        <LinearLayout
        android:weightSum="1"
        android:layout_weight=0.3
        horizontal>
    
            <TextView
            android:layout_weight=0.5
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />
    
            <TextView
            android:layout_weight=0.25
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />
    
            <TextView
            android:layout_weight=0.25
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />
    
        </LinearLayout>
    
        <SIMILAR LINEAR LAYOUTS HERE>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      父级的android:weightSum是可选的,因为默认设置为子级的layout_weight总和。完整代码如下:

      <?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">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="5dp"
          android:layout_marginEnd="5dp"
          android:layout_marginStart="5dp"
          android:layout_marginTop="5dp"
          android:orientation="horizontal">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="5dp"
              android:layout_marginStart="5dp"
              android:layout_weight="0"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
      </LinearLayout>
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="5dp"
          android:layout_marginEnd="5dp"
          android:layout_marginStart="5dp"
          android:orientation="horizontal">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="5dp"
              android:layout_marginStart="5dp"
              android:layout_weight="0"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
      </LinearLayout>
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="5dp"
          android:layout_marginEnd="5dp"
          android:layout_marginStart="5dp"
          android:orientation="horizontal">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="5dp"
              android:layout_marginStart="5dp"
              android:layout_weight="0"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0"
              android:background="@color/bpblack"
              android:text="AAAAA"
              android:textSize="30sp" />
      
      </LinearLayout>
      </LinearLayout>
      

      对于第二块和第三块,我指定 weight = 0。它们不再占用空间并占据内容的宽度,第一块占据所有内容。

      结果:

      【讨论】:

        【解决方案3】:

        您将 weightSum 设为 0.3,将 weight 设为 0.5、0.25 和 0.25,但所有 weight 的总和应等于 weightSum,以便适合屏幕。

        但在您的代码中,您的第一个 textview 点大于 weightsum,因此第一个文本本身将填满屏幕,

        根据您的屏幕,您可以为 textviews 指定 weightsum 为 4 和 weight 为 2,1,1,

        例如:

        <?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">
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="4">
        
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="#f00"
                    android:padding="1dp"
                    />
        
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#0f0"
                    android:padding="1dp"
                    />
        
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#00f"
                    android:padding="1dp"
                    />
            </LinearLayout>
        
            <!--your remaining layout here-->
        
        </LinearLayout>
        

        提供填充和背景只是为了在您的屏幕上显示差异。

        如果您想使用 3 个线性布局填充整个屏幕,那么您可以将 wightSum 指定为 3 用于根视图,并且可以将每个布局的权重指定为 1,如下所示,

        <?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:weightSum="3">
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="4">
        
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="#f00"
                    android:padding="1dp"
                    />
        
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#0f0"
                    android:padding="1dp"
                    />
        
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#00f"
                    android:padding="1dp"
                    />
            </LinearLayout>
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="4">
        
                <!--your text views here-->
            </LinearLayout>
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="4">
                <!--your text views here-->
        
            </LinearLayout>
        </LinearLayout>
        

        【讨论】: