簡體   English   中英

如何在 LinearLayout 中分隔項目?

[英]How to space out items in a LinearLayout?

我想創建一個如下所示的工具欄:

在此處輸入圖像描述

如何在 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" 將確保填充整個欄。

像這樣定義你的工具欄:

<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>

所有額外的空間都將分配給按鈕之間的透明視圖。

為線性布局中的項目添加 android:layout_margin="some value"。這對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM