Android 布局权重不起作用

Android layout weight doesn't work

我得到了这个布局:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="6sp"
    android:layout_marginLeft="16sp"
    android:layout_marginRight="6sp"
    android:layout_marginTop="6sp" >

    <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItem"
        android:inputType="none" />

    <TextView android:id="@+android:id/summary"
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_below="@android:id/title" 
        android:maxLines="2"
        android:layout_width="wrap_content" />

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<!-- android:id="@android:id/widget_frame" -->

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="6sp"
    android:layout_marginLeft="6sp"
    android:layout_marginRight="6sp"
    android:layout_marginTop="10sp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:weightSum="1" >

    <SeekBar
        android:id="@+id/change_seekbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:paddingLeft="20dip"
        android:paddingRight="20dip" >

    </SeekBar>

    <TextView
        android:id="@+id/Text_Box_seek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:textSize="16dip" >
    </TextView>
</LinearLayout>

我希望搜索栏占用内部线性布局中宽度的 80%,但搜索栏仍然很小.. 为什么它不占据 space 的其余部分?

尝试在 TextView- Text_Box_seek

中包含剩余的 20% 权重
 <TextView
        android:id="@+id/Text_Box_seek"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:textSize="16dip" >
    </TextView>

为了完美分配重量,您必须根据需要为所有项目分配重量。给别人设置wrap_content会让他们space忽略你给的权重

编辑

此外,正如 Phan Van Linh 所回答的那样,宽度应该是 match_parent 或任何指定的宽度,这样布局就不会根据其宽度(这将影响重量分布)调整内容。

LinearLayout 的布局宽度更改为 match_parent 或为其设置特定值。

解释一下 如果使用 LinearLayout width = wrap_content,LinearLayout 的宽度将取决于 childs.
并且您还设置了 child 的权重,因此 child 的宽度将取决于 parent.
=>(parent的宽度取决于childs,childs的宽度取决于parent)那么我认为View将不知道该做什么

当你使用 orientation :Horizo​​ntal..那时候 layout_width 应该是 0dp android:layout_width="0dp"

试试这个:

<!-- Preference should place its actual preference widget here. -->
<!-- android:id="@android:id/widget_frame" -->
<RelativeLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginBottom="6sp"
  android:layout_marginLeft="16sp"
  android:layout_marginRight="6sp"
  android:layout_marginTop="6sp">

  <TextView
    android:id="@+android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:textAppearance="?android:attr/textAppearanceListItem" />

  <TextView
    android:id="@+id/summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/title"
    android:maxLines="2"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<!-- android:id="@android:id/widget_frame" -->

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_marginBottom="6sp"
  android:layout_marginLeft="6sp"
  android:layout_marginRight="6sp"
  android:layout_marginTop="10sp"
  android:gravity="center_vertical"
  android:orientation="horizontal">

  <SeekBar
    android:id="@+id/change_seekbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:paddingLeft="20dip"
    android:paddingRight="20dip">

  </SeekBar>

  <TextView
    android:id="@+id/Text_Box_seek"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_weight="0.7"
    android:textSize="16dip"></TextView>
</LinearLayout>

</RelativeLayout>

将 LinearLayout 的宽度更改为 match_parent。

只需尝试将您的线性布局 属性 设置为 android:layout_width="match_parent",然后布局粗细即可。 希望这个回答对您有所帮助。

Ani 和 Shreya Patel 都对,只需在您的 LinearLayout 中将 layout_midth 更改为 match_parent 权重即可。