2015-04-04 47 views
0

我做了一个LinearLayout来包含几行按钮。要设置我在该行中可以使用的最大按钮数,我使用了6的权重总和和每个按钮的布局权重1,因此每行可以有6个按钮。但是,现在当我添加更多的按钮时,他们不断挤入同一行。任何人都知道为什么它不看布局的重量? 下面是我的布局代码:线性布局的权重总和不被强制

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:weightSum="6" 
android:clickable="false" 
android:orientation="horizontal"> 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/eng1" 
    android:id="@+id/eng1" 
    android:background="@color/red" 
    android:textStyle="bold" 
    android:layout_weight="1" 
    android:layout_margin="10dp" 
    android:clickable="true" 
    android:onClick="displayMessage" 
    android:textSize="20dp" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/cowl" 
    android:id="@+id/cowl" 
    android:textSize="20dp" 
    android:background="@color/red" 
    android:textStyle="bold" 
    android:clickable="true" 
    android:onClick="displayMessage" 
    android:layout_margin="10dp" 
    android:layout_weight="1" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/DIFFNG" 
    android:id="@+id/diffng" 
    android:textSize="20dp" 
    android:background="@color/red" 
    android:textStyle="bold" 
    android:clickable="true" 
    android:onClick="displayMessage" 
    android:layout_margin="10dp" 
    android:layout_weight="1" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/elec" 
    android:id="@+id/elec" 
    android:background="@color/red" 
    android:textStyle="bold" 
    android:clickable="true" 
    android:textSize="20dp" 
    android:onClick="displayMessage" 
    android:layout_margin="10dp" 
    android:layout_weight="1" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/battt" 
    android:id="@+id/battt" 
    android:background="@color/red" 
    android:textStyle="bold" 
    android:clickable="true" 
    android:textSize="20dp" 
    android:onClick="displayMessage" 
    android:layout_margin="10dp" 
    android:layout_weight="1" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/eng2" 
    android:id="@+id/eng2" 
    android:background="@color/red" 
    android:textStyle="bold" 
    android:clickable="true" 
    android:textSize="20dp" 
    android:onClick="displayMessage" 
    android:layout_margin="10dp" 
    android:layout_weight="1" /> 


</LinearLayout> 

回答

1

如您已设置weight_sum为6,你只能有一个布局内6次孩子的。如果添加超过6个,他们将被挤压。

把这个参数设置

android:layout_width="wrap_content" 
在所有按钮的

android:layout_width="0dp" 

,如果你是使用水平布局权重,你需要让孩子的作为0dp的宽度。

LinearLayout的高度为wrap_content。

+0

是否没有办法让它使用一个LinearLayout启动一个新的“行”? – AuthenticReplica 2015-04-04 14:46:24

+0

您需要在父LinearLayout(垂直)中使用不同的多重LinearLayout(水平)或使用GridView。 – 2015-04-04 14:47:56

+0

http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/ – 2015-04-04 14:48:00