2014-05-12 63 views
1

我想创建一个由8个按钮组成的布局(四行,两行一行)。使用按钮填充屏幕尺寸的布局

我希望按钮能够按比例填充屏幕大小 - 相等的边距,以便适合不同的屏幕尺寸。

这是我现在有:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<RelativeLayout 
    android:id="@+id/test_1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:layout_marginLeft="10dp" > 

    <Button 
     android:id="@+id/concession_btn" 
     android:layout_width="150dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton" /> 

    <Button 
     android:layout_width="150dip" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/concession_btn" 
     android:text="test2" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton" /> 

    <Button 
     android:id="@+id/test_2" 
     android:layout_width="150dip" 
     android:layout_below="@+id/concession_btn" 
     android:layout_height="wrap_content" 
     android:text="test3" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton"/> 

    <Button 
     android:layout_width="150dip" 
     android:layout_toRightOf="@+id/test_2" 
     android:layout_below="@+id/concession_btn" 
     android:layout_height="wrap_content" 
     android:text="test4" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton" /> 
    <Button 
     android:id="@+id/test_3" 
     android:layout_width="150dip" 
     android:layout_below="@+id/test_2" 
     android:layout_height="wrap_content" 
     android:text="test3" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton"/> 

    <Button 
     android:layout_width="150dip" 
     android:layout_toRightOf="@+id/test_3" 
     android:layout_below="@+id/test_2" 
     android:layout_height="wrap_content" 
     android:text="test4" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton" /> 
    <Button 
     android:id="@+id/test_4" 
     android:layout_width="150dip" 
     android:layout_below="@+id/test_3" 
     android:layout_height="wrap_content" 
     android:text="test3" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton"/> 

    <Button 
     android:layout_width="150dip" 
     android:layout_toRightOf="@+id/test_4" 
     android:layout_below="@+id/test_3" 
     android:layout_height="wrap_content" 
     android:text="test4" 
     android:layout_weight="1" 
     android:background="@drawable/mybutton" /> 
</RelativeLayout> 

这给了我八个按钮 - 连续4行中的两个,但我的XML使用固定的利润率所以不会FITT以及对不同势屏大小。

帮助将非常感谢!

+0

请尝试将布局和各分割布局插入按钮.. – Lal

+0

见我的回答@MichaelA – Lal

回答

2
// try this way,hope this will help you... 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/concession_btn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test1" 
      android:background="@drawable/mybutton" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test2" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/test_2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test3" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test4" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton" /> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/test_3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test5" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test6" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/test_4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test7" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="test8" 
      android:layout_weight="1" 
      android:background="@drawable/mybutton" /> 
    </LinearLayout> 
</LinearLayout> 
1

尝试将所述布局和各分割布局插入按钮..

要分割线性布局成两个相等的线性布局

<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
    <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
    //Your button here 
    </LinearLayout> 
    <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
    //Your button here 
    </LinearLayout> 
</LinearLayout> 

上面的代码将分割行一分为二。 。类似地粘贴上面的代码3次,以获得4行,每列2列...

请记住将上述代码括在LinearLayoutandroid:orientation="vertical"

放入内layouts..After您的按钮的位置将响应你的按钮..

+0

我想你的回答没有运气到目前为止 –

+0

发生了什么@MichaelA ??它不工作????当你写这段代码时发生了什么?布局是不是分裂? – Lal

+0

这种方法对我有用.. @迈克尔 – Lal

2

这是你的朋友布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight=".5" 
      android:text="test" /> 
    </LinearLayout> 

</LinearLayout> 
0

对于按钮,插入一个重量为1和0dp高度和宽度的视图元素完成这项工作。下面的代码片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="5dp"> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 1" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 2" 
      android:layout_weight="1"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
    </LinearLayout> 
    <View 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 3" 
      android:layout_weight="1"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button 4" 
      android:layout_weight="1"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
    </LinearLayout> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 


</LinearLayout> 

输出看起来是这样的:

Equally distributed buttons