Android TextView用法作为标签和值

25

我想将两个 TextView 分组并像标签和值一样使用。在 Android 中有没有可以将两个 TextView 分组的组件?如何在 Android 布局中实现此功能?

示例标签值


你的意思是除了将它们放在同一个布局中,还有其他需要吗?为什么你需要它们“分组”? - Ernir
不是的。从我看到的情况来看,您可以使用表格布局来实现它。 - Purush Pawar
2个回答

36

你可以使用<LinearLayout>将元素进行水平分组。此外,你应该使用样式来设置边距、背景和其他属性。这样可以避免为每个标签重复编写代码。

以下是一个例子:

<LinearLayout
                    style="@style/FormItem"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                <TextView
                        style="@style/FormLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/default_element_height"
                        android:text="@string/name_label"
                        />

                <EditText
                        style="@style/FormText.Editable"
                        android:id="@+id/cardholderName"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/default_element_height"
                        android:layout_weight="1"
                        android:gravity="right|center_vertical"
                        android:hint="@string/card_name_hint"
                        android:imeOptions="actionNext"
                        android:singleLine="true"
                        />
            </LinearLayout>

您可以基于上述布局创建自定义视图。 您看过创建自定义视图了吗?


2
您应该实现一个自定义列表视图,这样您只需定义一次布局,就可以为列表视图中的每一行绘制它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接