使用LinearLayout将TextView项的宽度与具有不同父项的项的宽度匹配

时间:2017-06-29 21:56:19

标签: android xml android-layout android-linearlayout

是否可以将具有一个父项的项的宽度与具有另一个父项的项的宽度相匹配?

我想基本上创建一个表格,其中上部包含一个LinearLayout,其中包含水平TextViews,包含标签,基本上是一行,另一个是滚动的LinearLayout,其水平TextViews包含在应用程序中更新的值。我希望标签行具有固定宽度的textviews和结果textviews具有相同的宽度。现在我的标签宽度为wrap_content,这就是我想要的。如何在第二个布局中使TextViews具有相同的宽度,即使内容将被更改?

简单地说:我希望表兄文本视图具有相同的宽度,而一个堂兄要复制另一个。

下面是我的.xml代码。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:orientation="horizontal">



    <TextView
        android:id="@+id/resultsLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Results" />

    <TextView
        android:id="@+id/latLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Latitude" />

    <TextView
        android:id="@+id/longLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Longitude" />

    <TextView
        android:id="@+id/timeLabel"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Time" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    android:background="@android:color/background_dark">

    <TextView
        android:id="@+id/resultsValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/latValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/longValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/timeValue"
        android:layout_margin="1dp"
        android:background="@color/cardview_light_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        />


</LinearLayout>

2 个答案:

答案 0 :(得分:1)

TableLayout就是你要找的,试试这个:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/background_dark"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:id="@+id/resultsLabel"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Results" />

        <TextView
            android:id="@+id/latLabel"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Latitude" />

        <TextView
            android:id="@+id/longLabel"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Longitude" />

        <TextView
            android:id="@+id/timeLabel"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Time" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/resultsValue"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/latValue"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/longValue"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/timeValue"
            android:layout_margin="1dp"
            android:background="@color/cardview_light_background"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" />
    </TableRow>
</TableLayout>

答案 1 :(得分:0)

我添加了LinearLayout嵌套,其中一个主要的LinearLayout垂直面向表,然后水平地为列,然后再垂直再次保存两个textViews:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.stardustscanner.LogData">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_dark"
        android:orientation="horizontal">

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

            <TextView
                android:id="@+id/resultsLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center"
                android:text="Results" />

            <TextView
                android:id="@+id/resultsValue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center" />

        </LinearLayout>

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

            <TextView
                android:id="@+id/latLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center"
                android:text="Latitude" />

            <TextView
                android:id="@+id/latValue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center" />

        </LinearLayout>

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

            <TextView
                android:id="@+id/longLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center"
                android:text="Longitude" />

            <TextView
                android:id="@+id/longValue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center" />

        </LinearLayout>

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

            <TextView
                android:id="@+id/timeLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center"
                android:text="Time" />

            <TextView
                android:id="@+id/timeValue"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/cardview_light_background"
                android:gravity="center" />

        </LinearLayout>

    </LinearLayout>

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

        <Button
            android:layout_width="match_parent"
            android:layout_margin="15dp"
            android:layout_height="wrap_content"
            android:text="Clear Log"/>

    </LinearLayout>


</LinearLayout>