2011-11-28 60 views
3

我有這樣的佈局三個按鈕 - >調整大小以適應父寬度

  • 線性佈局
    • 滾動查看
      • 相對佈局
        • 9X按鈕

在View這樣的(3×3格)

+---------+ 
| o o o | 
| o o o | 
| o o o | 
+---------+ 

每個按鈕都有它的背景,沒有文字,背景是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/menu_btn1_hover" android:state_pressed="true"/> 
    <item android:drawable="@drawable/menu_btn1"/> 
</selector> 

我應該如何陳述的佈局,所以按鈕將始終爲每行3個,並將調整大小以便它們適合視圖?

+0

看起來像使用TableLayout的完美場所。 –

+0

LinearLayout頂部的ScrollView中的TableLayout? –

+0

ScrollView中的TableLayout而不是RelativeLayout我寧願說。 –

回答

12

試試這個!

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <Button android:id="@+id/Button04" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> 
     <Button android:id="@+id/Button05" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> 
     <Button android:id="@+id/Button06" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> 
    </LinearLayout> 
    <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <Button android:id="@+id/Button01" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> 
     <Button android:id="@+id/Button02" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> 
     <Button android:id="@+id/Button03" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> 
    </LinearLayout> 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <Button android:id="@+id/button1" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button> 
     <Button android:id="@+id/button2" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button> 
     <Button android:id="@+id/button3" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button> 
    </LinearLayout> 

</LinearLayout> 

截圖:

enter image description here

UPDATE:

如果您的LinearLayout(機器人:ID = 「@ + ID /包裝」(LOOK下面的代碼))具有相同的layout_height和layout_width你會得到你想要的東西

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:id="@+id/wrapper" 
     android:layout_width="300dp" android:weightSum="1" 
     android:orientation="vertical" android:layout_height="300dp"> 
     <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp"> 
      <Button android:id="@+id/Button04" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> 
      <Button android:id="@+id/Button05" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> 
      <Button android:id="@+id/Button06" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> 
     </LinearLayout> 
     <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp"> 
      <Button android:id="@+id/Button01" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> 
      <Button android:id="@+id/Button02" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> 
      <Button android:id="@+id/Button03" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> 
     </LinearLayout> 
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp"> 
      <Button android:id="@+id/button1" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button> 
      <Button android:id="@+id/button2" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button> 
      <Button android:id="@+id/button3" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

請看看截圖。在第二個屏幕中的LinearLayout與Android:ID = 「@ + ID /包裝」 具有相同的寬度和高度,等於300dp

enter image description hereenter image description here

試試吧,請!希望它能幫助你!

+0

它與Kasper的回答有相同的問題。按鈕水平調整大小,但垂直調整高度。需要用相同的比例將它們拉伸到高度,如寬度 –

+0

對,你做到了,很抱歉接受這麼晚回答:))謝謝 –

+0

謝謝親愛的。您的解決方案非常簡單和智能! – mghhgm

1

您可以使用填充屏幕並將其嵌套在ScrollView中的TableLayout。然後,您可以通過編程方式充氣TableRow中的3個視圖,並將其添加到TableLayout,就像這樣,您將獲得每行3個視圖。 重複這3次,你得到你所要求的。

請注意,如果你這樣做編程,您可以更改按鍵很容易

好運的順序, Arkde

2

這應該做的伎倆,如果你與你提到RelativeLayout更換。所有你需要做的是ID和風格:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 
     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 
+0

幾乎完美的工作,但我的按鈕是方形的,他們的寬度總和(3×寬度)比屏幕更長。使用'layout_weight'我可以自動拉伸他們以適應寬度,但高度不調整。 –

+0

假設'ScrollView'具有固定的高度,您可以將我發佈的佈局中的所有'android:layout_height'更改爲'fill_parent',並且它們也會高度拉伸。我所有的意思是全部,包括外部'LinearLayout'中的一個。 – kaspermoerch

+0

滾動視圖在兩個維上都是fill_parent。和'android:layout_height'只在LinearLayout或兩者上(佈局和按鈕)? –

相關問題