当前位置 : 主页 > 手机开发 > android >

如何在LinearLayout中分隔出项目?

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想创建一个看起来像这样的工具栏: 我怎么能用XML做到这一点?这是我目前的样子: LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbarLinearLayout"
我想创建一个看起来像这样的工具栏:

我怎么能用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”将确保填充整个栏.

网友评论