我经常在Android文档中读到这个有趣的权重值。 现在我想第一次尝试一下,但它根本不行。

我从文档中了解到这样的布局:

  <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

  </LinearLayout>

应该创建两个水平对齐的按钮,并平等地共享空间。问题是这两个按钮不能填满空间。

我希望按钮增长,并填满整个行。如果两个按钮都设置为匹配父按钮,则只显示第一个按钮并填充整行。


当前回答

以下是代码中的更改(用粗体标记):

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="0dp" //changes made here
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" /> //changes made here

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="0dp" //changes made here
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" /> //changes made here

  </LinearLayout>

因为你的线性布局有水平方向,因此你需要保持你的宽度仅为0dp。在这个方向上使用权重。(如果你的方向是垂直的,你将保持你的高度只有0dp)。

因为有2个视图,你已经放置了android:layout_weight="1"的两个视图,这意味着它将在水平方向(或宽度)平分两个视图。

其他回答

它的android: layout_weight。权重只能在线性布局中使用。如果线性布局的方向是垂直的,那么使用android:layout_height="0dp",如果方向是水平的,那么使用android:layout_width ="0dp"。它会完美工作的。

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/logonFormButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"       
        android:orientation="horizontal">

        <Button
            android:id="@+id/logonFormBTLogon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            android:text="@string/logon"
            android:layout_weight="0.5" />

        <Button
            android:id="@+id/logonFormBTCancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            android:text="@string/cancel"
            android:layout_weight="0.5" />
    </LinearLayout>

您没有设置layout_weight属性。你的代码读weight="1",它应该读android:layout_weight="1"。

另外,你需要添加这个android:layout_width="0dp"为儿童视图[按钮视图]的LinerLayout

也许将两个按钮的layout_width属性设置为“fill_parent”就可以了。

我刚刚测试了这段代码,它在模拟器中工作:

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

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello world"/>

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="goodbye world"/>

</LinearLayout>

确保在两个按钮上都将layout_width设置为“fill_parent”。