2012-09-08 43 views
1

我想創建菜單佈局,我可以在兩個相同寬度的柱子中放置按鈕。
我不想拉伸按鈕來填充所有屏幕(除非有很長的標籤)。
我希望所有的按鈕具有相同的寬度 - 最長標籤的按鈕寬度。
我試圖在一個水平線性佈局內的兩個垂直線性佈局。
我發現的所有解決方案都建議對垂直線性佈局使用相同的layout_weight,對於強制按鈕佔據整個屏幕寬度的水平佈局,使用layout_width = fill_parent。
我的問題有什麼常見的解決方案嗎?

這是我所期待的 - https://dl.dropbox.com/u/2751770/android-layout1.PNG
這是解決方案如何共同樣子 - https://dl.dropbox.com/u/2751770/android-layout2.PNG

這裏是代碼,我使用的是作爲一個共同的解決方案:android - 用相同的寬度放置的視圖沒有填充所有屏幕

<LinearLayout style="@style/menuLayout" 
       android:layout_width="match_parent" 
       android:orientation="horizontal" 
       android:baselineAligned="false"> 
    <LinearLayout android:orientation="vertical" 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content"> 
     <Button style="@style/menuButton" 
       android:text="Title 1"/> 
     <Button style="@style/menuButton" 
       android:text="Title 2"/> 
    </LinearLayout> 
    <LinearLayout android:orientation="vertical" 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content"> 
     <Button style="@style/menuButton" 
       android:text="Title 3"/> 
     <Button style="@style/menuButton" 
       android:text="Long title 4"/> 
    </LinearLayout> 
</LinearLayout> 

樣式:

<style name="menuLayout"> 
    <item name="android:layout_gravity">center</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_width">wrap_content</item> 
</style> 
<style name="menuButton"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
    <item name="android:paddingLeft">30dp</item> 
    <item name="android:paddingRight">30dp</item> 
    <item name="android:layout_margin">5dp</item> 
</style> 
+0

其實你能否清楚你的問題究竟是什麼?現在發生了什麼?並給出屏幕截圖並粘貼您的代碼,以便我們可以改進它 –

回答

0

您正在設置樣式「menu_layout」中的寬度,並且也在第二個裏面你的代碼中的一個。刪除其中一個。並嘗試使用LinearLayout中的android:weightSum="1",然後在每個LinearLayout中使用android:layout_weight="0.5"

相關問題