2017-03-06 45 views
1

我得到这个布局:Android的布局重量不工作

<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%在内部线性布局,但寻求酒吧保持非常小..为什么不采取其余的空间?

回答

3

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

解释 如果您使用的LinearLayout宽度= W rap_content,LinearLayout的宽度将取决于孩子。
而且你还设置了孩子的体重,所以孩子的宽度将取决于父母。
=>(父母的宽度取决于孩子,孩子的宽度取决于父母)然后我认为视图将不知道该怎么办

1

尝试,包括20%的重量在TextView休息 - Text_Box_seek

<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会让他们占用空间而忽略你给出的重量。

EDIT

同样如藩范灵回答宽度应match_ parent或任何指定的,使得布局不会在其内容dpened其宽度(这会影响重量分布。

0

当您使用方向时:Horizo​​ntal..at那时候layout_width应该是0dp机器人:layout_width = “0dp”

0

尝试此,

<!-- 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> 
0

只是尝试将您的线性布局属性设置为android:layout_width="match_parent",那么布局权重将起作用。 希望这个答案能帮到你。

0

Ani和Shreya Patel都是对的,只需要在您的LinearLayout中将layout_midth更改为match_parent并且权重可以工作。