我想将两个 TextView 分组并像标签和值一样使用。在 Android 中有没有可以将两个 TextView 分组的组件?如何在 Android 布局中实现此功能?
我想将两个 TextView 分组并像标签和值一样使用。在 Android 中有没有可以将两个 TextView 分组的组件?如何在 Android 布局中实现此功能?
你可以使用<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>
您可以基于上述布局创建自定义视图。 您看过创建自定义视图了吗?