【发布时间】:2019-03-28 10:26:12
【问题描述】:
我正在使用水平线性布局。
在同一行中,我想在左侧放置一个按钮,将文本放在中间,并将另一个按钮放在右侧。
如何实现?
【问题讨论】:
-
到目前为止你尝试了什么?
-
由于这是 UI 任务,请通过图片向我们展示您想要完成的任务以及目前取得的成果。
标签: android layout alignment placement
我正在使用水平线性布局。
在同一行中,我想在左侧放置一个按钮,将文本放在中间,并将另一个按钮放在右侧。
如何实现?
【问题讨论】:
标签: android layout alignment placement
同样的 XML 方法:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum=".9"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="B1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="B2"
/>
</LinearLayout>
如果您想调整单个项目的宽度,可以更改 'android:layout_weight' 属性
【讨论】:
要在 LinearLayout 中均匀分布元素,您需要使用 weight。你可以这样做:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_default"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Text"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Text"
/>
</LinearLayout>
【讨论】:
android:gravity="center"