Android线性布局。2行,共2行

Android线性布局。2行,共2行,android,android-linearlayout,Android,Android Linearlayout,我有一个线性布局,4个文本视图(2个标签,2个带数据),结构如下 Label Label Data Data 我想把上面的数字加倍,但我有很大的困难。 欲望是 Label Label Data Data Label Label Data Data 这是我到目前为止的代码。当我添加更多文本视图时,它会抛出定位 <LinearLayout android:id="@+id/linearLayout" android:layo

我有一个线性布局,4个文本视图(2个标签,2个带数据),结构如下

Label    Label
Data     Data
我想把上面的数字加倍,但我有很大的困难。 欲望是

Label    Label
Data     Data
Label    Label
Data     Data
这是我到目前为止的代码。当我添加更多文本视图时,它会抛出定位

    <LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/temperatureLabel"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="100" android:baselineAligned="false">

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

        <TextView
            android:id="@+id/label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/label"
            android:textColor="#80ffffff" />

        <TextView
            android:id="@+id/data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/data"
            android:textColor="@android:color/white"
            android:textSize="24sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/label"
            android:textColor="#80ffffff" />

        <TextView
            android:id="@+id/data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/data"
            android:textColor="@android:color/white"
            android:textSize="24sp" />
    </LinearLayout>
</LinearLayout>

如果您计划将其翻倍或采用类似表格的布局。您可以使用gridLayout来解决您的问题

例如


如果您计划将其翻倍或采用类似表格的布局。您可以使用gridLayout来解决您的问题

例如


为了避免嵌套布局,可以使用约束布局并为每个视图设置水平和垂直约束,以获得所需的位置

<ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/label1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label1"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/data1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data1"
    app:layout_constraintStart_toStartOf="@+id/label1"
    app:layout_constraintTop_toBottomOf="@+id/label1" />

<TextView
    android:id="@+id/label2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label2"
    app:layout_constraintLeft_toRightOf="@id/label1"/>

<TextView
    android:id="@+id/data2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data2"
    app:layout_constraintStart_toStartOf="@+id/label2"
    app:layout_constraintTop_toBottomOf="@+id/label2" />

<TextView
    android:id="@+id/label3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label3"
    app:layout_constraintTop_toBottomOf="@id/data1"
    app:layout_constraintStart_toStartOf="@+id/label1" />

<TextView
    android:id="@+id/data3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data3"
    app:layout_constraintStart_toStartOf="@+id/label3"
    app:layout_constraintTop_toBottomOf="@+id/label3" />

<TextView
    android:id="@+id/label4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label4"
    app:layout_constraintTop_toBottomOf="@id/data2"
    app:layout_constraintStart_toStartOf="@id/label2" />

<TextView
    android:id="@+id/data4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data4"
    app:layout_constraintStart_toStartOf="@+id/label4"
    app:layout_constraintTop_toBottomOf="@+id/label4" />

为了避免嵌套布局,可以使用约束布局并为每个视图设置水平和垂直约束,以获得所需的位置

<ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/label1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label1"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/data1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data1"
    app:layout_constraintStart_toStartOf="@+id/label1"
    app:layout_constraintTop_toBottomOf="@+id/label1" />

<TextView
    android:id="@+id/label2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label2"
    app:layout_constraintLeft_toRightOf="@id/label1"/>

<TextView
    android:id="@+id/data2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data2"
    app:layout_constraintStart_toStartOf="@+id/label2"
    app:layout_constraintTop_toBottomOf="@+id/label2" />

<TextView
    android:id="@+id/label3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label3"
    app:layout_constraintTop_toBottomOf="@id/data1"
    app:layout_constraintStart_toStartOf="@+id/label1" />

<TextView
    android:id="@+id/data3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data3"
    app:layout_constraintStart_toStartOf="@+id/label3"
    app:layout_constraintTop_toBottomOf="@+id/label3" />

<TextView
    android:id="@+id/label4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Label4"
    app:layout_constraintTop_toBottomOf="@id/data2"
    app:layout_constraintStart_toStartOf="@id/label2" />

<TextView
    android:id="@+id/data4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data4"
    app:layout_constraintStart_toStartOf="@+id/label4"
    app:layout_constraintTop_toBottomOf="@+id/label4" />



你能把你想要的截图放出来吗?前后你想要的代码是什么?在结构更新之前和之后是什么样子的?您的视图有相同的id-这一个应该是固定的-编译错误。第一个线性布局有android:layout\u height=“match\u parent”第一个线性布局的结果是可见的,第二个是不可见的。你能把你想要的截图放出来吗?前后你想要的代码是什么?在结构更新之前和之后是什么样子的?您的视图有相同的id-这一个应该是固定的-编译错误。第一个线性布局有android:layout\u height=“match\u parent”第一个线性布局的结果可见,第二个不起作用。Android给了我关于布局行的错误,以及我发布的代码中缺少结束标记的所有错误。你能再试一次吗?我测试过了,在这附近效果很好,明白了。您必须在Gradle文件中添加以下行编译'com.android.support:gridlayout-v7:25.3.1'或同一库的更新版本,非常感谢。如果你不介意的话,还有一个问题。将Elemenet向下移动的最佳方式是什么。我更新了答案以帮助您实现这一点。基本上,您的网格布局需要位于另一个布局内,例如宽度和高度均为“匹配父级”的FrameLayout,然后您可以将网格布局的布局重力设置为“底部不工作”。Android给了我关于布局行的错误,以及我发布的代码中缺少结束标记的所有错误。你能再试一次吗?我测试过了,在这附近效果很好,明白了。您必须在Gradle文件中添加以下行编译'com.android.support:gridlayout-v7:25.3.1'或同一库的更新版本,非常感谢。如果你不介意的话,还有一个问题。将Elemenet向下移动的最佳方式是什么。我更新了答案以帮助您实现这一点。基本上,您的网格布局需要位于另一个布局内,例如宽度和高度均为匹配父级的FrameLayout,然后您可以将网格布局的布局重力设置为底部