2011-05-23 76 views
3

我想創建一個工具欄,看起來像:如何在LinearLayout中分隔項目?

enter image description here

我怎樣才能做到這一點的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> 

回答

4

看起來你可能想要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」將確保整個酒吧被填充。

+0

這將填補它周圍。這很好,但我希望左右的距離等距離,更像上圖那樣突出。 – 2011-05-23 23:01:55

+0

我試着添加paddingLeft&paddingRight,但是按鈕之間的空間沒有被填充 – 2011-05-23 23:05:53

+0

嘿,我已經稍微更新了我的答案 - 希望這更符合您所尋找的內容。 – actionshrimp 2011-05-23 23:09:42

3

定義工具欄是這樣的:

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

所有多餘的空間將被分配到按鈕之間的透明的意見。

0

爲linearlayout中的項目添加android:layout_margin =「some value」。這對我很有用。

相關問題