问题描述
|
我想创建一个如下所示的工具栏:
如何在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的padding属性,例如
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 \”将确保填充整个栏。
,定义工具栏,如下所示:
<LinearLayout ...>
<Button android:id=\"@+id/replyButton\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
...
/>
<View
android:layout_width=\"0dp\"
android:layout_height=\"fill_parent\"
android:layout_weight=\"1\"
/>
<Button ...
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
/>
<!-- etc. -->
</LinearLayout>
所有多余的空间将分配给按钮之间的透明视图。
,为linearlayout中的项目添加android:layout_margin = \“一些值\”。这对我有用。