我经常在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>

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

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


当前回答

用fill_parent替换wrap_content。

其他回答

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

尝试将两个按钮的layout_width设置为“0dip”,并将两个按钮的权重设置为0.5

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

<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"的两个视图,这意味着它将在水平方向(或宽度)平分两个视图。

你必须这样写:为我工作

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

         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_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"
            android:layout_weight="1" />

要记住3件事:

设置child的android:layout_width为"0dp" 设置父类的android:weightSum (编辑:正如Jason Moore注意到的,这个属性是可选的,因为默认情况下它被设置为子节点的layout_weight总和) 按比例设置每个子的android:layout_weight(例如weightSum="5",三个子:layout_weight ="1", layout_weight="3", layout_weight="1")

例子:

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

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

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

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

</LinearLayout>

结果是: