2017-03-06 41 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並且權重可以工作。