Android 使用LinearLayout将TextView项的宽度与具有不同父项的项的宽度匹配
是否可以将一个父项的项目宽度与另一个父项的项目宽度匹配 我想制作一个表格,上面部分包含一个线性布局,其中水平文本视图包含标签,基本上是一行和另一个滚动启用的线性布局,水平文本视图包含应用程序中更新的值。我希望标签行具有固定宽度的TextView,结果TextView具有相同的宽度。现在我有包装内容的标签宽度,这是我想要的。如何使第二个版面中的文本视图具有相同的宽度,即使内容将被更改 简单地说:我希望堂兄文本视图具有相同的宽度,一个堂兄复制另一个 下面是我的.xml代码Android 使用LinearLayout将TextView项的宽度与具有不同父项的项的宽度匹配,android,xml,android-layout,android-linearlayout,Android,Xml,Android Layout,Android Linearlayout,是否可以将一个父项的项目宽度与另一个父项的项目宽度匹配 我想制作一个表格,上面部分包含一个线性布局,其中水平文本视图包含标签,基本上是一行和另一个滚动启用的线性布局,水平文本视图包含应用程序中更新的值。我希望标签行具有固定宽度的TextView,结果TextView具有相同的宽度。现在我有包装内容的标签宽度,这是我想要的。如何使第二个版面中的文本视图具有相同的宽度,即使内容将被更改 简单地说:我希望堂兄文本视图具有相同的宽度,一个堂兄复制另一个 下面是我的.xml代码 <LinearLay
<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>
android:background=“@android:color/background\u dark”>
您需要的是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>
我添加了LinearLayout嵌套,其中主LinearLayout在表格中垂直排列,然后在列中水平排列,然后再次垂直排列以容纳两个文本视图:
<?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>
谢谢您的回复。我以前从未使用过TableLayout,我可以将其配置为滚动第2行并继续滚动,同时将第1行保留在屏幕上吗?好问题。现在这将是真正的挑战,如果您将文本视图包装在第二行,在ScrollView中,您可以分别滚动它们。可能不是你想要的,但这是我现在能想到的最好的。然后,通过一点编码,你可能可以链接滚动。这是一个很好的挑战。也许我有时间的时候会试试。