布局权重对于嵌套线性布局无法正常工作

时间:2017-06-18 12:23:04

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

我想要附加像enter image description here

这样的活动

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

<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被堆叠到最左边,根本没有宽度。我不确定会出现什么问题。当我把所有三个相同重量的视图放在一起时,它们会很好地水平叠加。

3 个答案:

答案 0 :(得分:0)

如果您同时提供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>

答案 1 :(得分:0)

父级的

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>

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

结果:

enter image description here

答案 2 :(得分:0)

你给予weightSum为0.3,权重为0.5,0.25和0.25,但所有权重的总和应该等于weightSum,以便它适合屏幕。

但是在你的代码中,你的第一个textview点比权重大,所以第一个文本本身将填满屏幕,

根据您的屏幕,您可以为textview提供权重4和权重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>