2011-11-28 58 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或两者上(布局和按钮)? –

相关问题