具有相同权重的不同布局具有不同的大小

时间:2014-07-16 12:20:21

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

在我的布局中,我有两个不同的LinearLayouts。第一个是:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:gravity="center"
            android:text="@string/lblPais" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblJ" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblG" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblP" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPF" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPC" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblDif" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPuntos" />

    </LinearLayout>

第二个就是这个布局:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="15" >

        <TextView
            android:id="@+id/lblPais1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:gravity="center"
            android:text="@string/lblPais" />

        <TextView
            android:id="@+id/lblJ1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblG1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblP1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblPF1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblPC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblDif1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPtsVacio" />

        <TextView
            android:id="@+id/lblPuntos1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPtsVacio" />

    </LinearLayout>

正如您所看到的,两个布局都为layout_width属性(match_parent)获取了相同的值。在两个布局中,我有相同数量的元素(8 TextView)。除了第一个TextView之外,每个TextView的权重都相同,权重为8。

如果两个布局相同(只是TextViews上的标签不同),为什么它们有不同的尺寸?

5 个答案:

答案 0 :(得分:3)

请尝试这种方式,希望这有助于您解决问题。

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:gravity="center"
            android:text="@string/lblPais" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblJ" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblG" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblP" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPF" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPC" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblDif" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPuntos" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/lblPais1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:gravity="center"
            android:text="@string/lblPais" />

        <TextView
            android:id="@+id/lblJ1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblG1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblP1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblPF1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblPC"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lbl0" />

        <TextView
            android:id="@+id/lblDif1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPtsVacio" />

        <TextView
            android:id="@+id/lblPuntos1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/lblPtsVacio" />

    </LinearLayout>
</LinearLayout>

答案 1 :(得分:1)

如果您希望所有布局与weight

相同

你的orientation是垂直的,所以改变......

android:layout_height="wrap_content"

android:layout_height="0dp"

全部TextView

答案 2 :(得分:1)

您尚未在第一个linearlayout中提供android:weightSum="15"

答案 3 :(得分:0)

您必须在第一个父版面上添加andoid:weightSum="15",所有内容都应该落实到位。

答案 4 :(得分:0)

将父线性布局高度设为 android:layout_height="match_parent"

相关问题