安卓代码:线性布局,将四个按钮放置在同一行且均分
时间: 2024-03-17 16:41:54 浏览: 17
可以 `LinearLayout` 和 `layout_weight` 属性来实现将四个按钮均分放置在同一行的布局。下面是示例代码:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 3" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 4" />
</LinearLayout>
```
在上述代码中,我们使用了 `LinearLayout` 的 `orientation` 属性设置为 `horizontal`,表示子视图将会水平排列。然后,我们将每个 `Button` 的 `layout_width` 属性设置为 `0dp`,并使用 `layout_weight` 属性设置为 `1`,这样每个按钮都会占据相等的空间,从而实现了均分的效果。