【问题标题】:Matching Width of TextView Items to Widths of Items with Different Parents using LinearLayout使用 LinearLayout 将 TextView 项的宽度与具有不同父项的项的宽度匹配
【发布时间】:2017-06-29 21:56:19
【问题描述】:

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

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

简单地说:我希望表亲 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>

【问题讨论】:

    标签: android xml android-layout android-linearlayout


    【解决方案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>
    

    【讨论】:

    • 感谢您的回复。我以前从未使用过 TableLayout,我可以将其配置为滚动第 2 行并继续滚动,同时保持屏幕上的第 1 行吗?
    • 好问题。现在这将是真正的挑战 xD
    • 如果您将 TextViews 包装在第二行,在 ScrollView 中,您可以分别滚动它们中的每一个。可能不是您想要的,但这是我现在能想到的最好的。
    • 然后通过一些编码,您可能可以链接滚动。这是一个很好的挑战。也许我有时间会试一试。
    【解决方案2】:

    我添加了 LinearLayout 嵌套,其主要 LinearLayout 垂直面向表格,然后水平面向列,然后再次垂直以容纳两个 textView:

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      相关资源
      最近更新 更多