我想创建一个如下所示的工具栏:
我如何在 XML 中执行此操作?这是我目前的样子:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:background="@color/solid_yellow">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/replyButton" android:text="Reply"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RT" android:id="@+id/rtButton"></Button>
<Button android:id="@+id/dmButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DM"></Button>
</LinearLayout>
最佳答案
看起来您可能需要 LinearLayout 的填充属性,例如
android:padding="5dip"
要让每个项目占用相同数量的空间,请使用 layout_weight,例如
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout" android:background="@color/solid_yellow">
<Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/replyButton" android:text="Reply"></Button>
<Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="RT" android:id="@+id/rtButton"></Button>
<Button android:id="@+id/dmButton" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="DM"></Button>
</LinearLayout>
只要所有元素具有相同的权重,它们就应该占用相同的空间。 layout_width="fill_parent"将确保整个栏都被填满。
关于java - 如何在 LinearLayout 中分隔项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103960/