【发布时间】:2013-12-11 23:41:07
【问题描述】:
我的 android 应用程序中有一个表单,它由一个垂直方向的父 LinearLayout 和一堆水平方向的子元素组成。孩子们有一个标签 (TextView),然后是一个 EditText。包装每个表单行的 LinearLayout 设置为具有 match_parent 的宽度,因此它填充了屏幕的宽度。包含标签和编辑文本的内部表单行布局大小为 30% 标签,70% 文本字段。我有一个字段,虽然它有一个标签(textview),编辑文本,然后在同一行上有另一个文本视图。我希望标签保留 30% 的空间,固定该行末尾的最后一个文本视图,并使用 edittext 占用剩余空间。这是代码示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout style="@style/FormRow">
<TextView
android:id="@+id/Username"
style="@style/FormRowLabel"
android:text="@string/username_hint"
/>
<EditText
android:id="@+id/UsernameValue"
style="@style/FormRowEditText"
android:inputType="textEmailAddress"
/>
</LinearLayout>
.....
<LinearLayout style="@style/FormRow">
<TextView
android:id="@+id/AnotherField"
style="@style/FormRowLabel"
android:text="@string/fieldname"
/>
<EditText
android:id="@+id/AnotherFieldValue"
style="@style/FormRowEditText"
android:width="?"
/>
<TextView layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sometext"
/>
</LinearLayout>
</LinearLayout>
我的风格是这样的:
<style name="FormRow">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<item name="android:paddingTop">6dp</item>
<item name="android:paddingBottom">6dp</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
<item name="android:weightSum">10</item>
</style>
<style name="FormRowLabel">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_marginLeft">2dp</item>
<item name="android:layout_weight">3</item>
<item name="android:singleLine">false</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
<style name="FormRowEditText" parent="EditText">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">7</item>
</style>
我已经尝试过为所有 3 个使用权重,但在横向上,我最终会为可用于编辑文本的文本视图留出一大堆空间。有没有办法用布局权重来做到这一点?或者我还能怎么做?我不是相对布局的忠实粉丝,所以我更愿意将这些内部布局保留为 LinearLayouts。
【问题讨论】:
-
你试过TableLayout吗?
-
我认为Table API Guide 会帮助你:)
标签: android android-layout android-linearlayout