2013-01-09 32 views
0

我有一个观点,它看起来像这样ATM:布局的weight_sum申请90%的观点

enter image description here

我想,首先3个黑色行填写〜屏幕的70%。不管什么将是屏幕大小。我认为我可以通过为布局分配权重来实现它,似乎并不那么容易。正如你所看到的,我已经用按钮实现了它们,它们总是伸展到宽度。

所以,这里是我为这个XML代码:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"   
    android:orientation="vertical" 
    android:id="@+id/main" 
    android:weightSum="10" //mean summary 100% 

    > 
<LinearLayout 
    android:id="@+id/lin1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="3"  
    android:layout_weight="2.3" //23% for each row 
    > 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Layout Normal" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad2" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad3" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/lin2" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_marginTop="2dp" 
    android:orientation="horizontal" 
    android:weightSum="3" 
    android:layout_weight="2.3" > 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad4" /> 

    <Button 
     android:id="@+id/button5" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad5" /> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad6" /> 
</LinearLayout> 



<LinearLayout 
    android:id="@+id/lin3" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_below="@id/lin2" 
    android:orientation="horizontal" 
    android:weightSum="3" 
    android:layout_weight="2.3" > 

    <Button 
     android:id="@+id/button7" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad7" /> 


    <Button 
     android:id="@+id/button8" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad8" /> 

    <Button 
     android:id="@+id/button9" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/greenbtn" 
     android:onClick="onClick" 
     android:text="Pad9" /> 
</LinearLayout> 

    <LinearLayout 
    android:id="@+id/lin5" 
    android:layout_width="fill_parent" 
    android:layout_below="@id/lin3" 
    android:layout_height="60dp" 
    android:layout_marginBottom="5dp" 
    android:layout_marginTop="10dp" 
    android:orientation="horizontal" 
    android:layout_weight="2.1" > //21% for toggles 

    <ToggleButton 
     android:id="@+id/toggleButton1" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:onClick="onClick" 
     android:textOff="Loop1 Off" 
     android:textOn="Loop1 ON" /> 

    <ToggleButton 
     android:id="@+id/toggleButton2" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:onClick="onClick" 
     android:textOff="Loop2 Off" 
     android:textOn="Loop2 ON" /> 

    <ToggleButton 
     android:id="@+id/toggleButton3" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:onClick="onClick" 
     android:textOff="Loop3 Off" 
     android:textOn="Loop3 ON" /> 

    <ToggleButton 
     android:id="@+id/toggleButton4" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:onClick="onClick" 
     android:textOff="Loop4 Off" 
     android:textOn="Loop4 ON" /> 
</LinearLayout> 


<LinearLayout 
    android:id="@+id/lin6" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="1"  //10% for webview 
    > 

    <WebView 
     android:id="@+id/webView" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 

    /> 
</LinearLayout> 
</LinearLayout> 

正如你所看到的,我的想法,我已经给了每个小排黑屏幕的23%(23x3 = 69%),用于切换21%,webview 10%。

任何帮助将不胜感激。

回答

1

@Daler你想使用的重量平衡的高度,所以你必须设置layout_height到0dp

也,在我看来,你使用太多的LinearLayouts,也许你可以消除很少在内存中保存,处理时间,响应速度。我猜这样的东西:

<Linear Layout - vertical - match_parent > 

    // 0.269 * 3 = 0.7 
    <linear layout 1 - horizontal weight = 0.269 layout_height=0dp> the three views </> 
    <linear layout 2 - horizontal weight = 0.269 layout_height=0dp> the three views </> 
    <linear layout 3 - horizontal weight = 0.269 layout_height=0dp> the three views </> 
    // the other 30% 
    <linear layout 4 - horizontal weight = 0.15 layout_height=0dp> the three views </> 
    <linear layout 5 - horizontal weight = 0.15 layout_height=0dp> the three views </> 


</end the top linear layout> 

这是假设它是3行在黑色区域应该是70%。这就是我从你的形象中了解到的。

+0

不幸的是,没有任何改变。仍然如上所述。 – Daler

+0

顶层布局的高度为'wrap_content',所以更改为'fill_parent'并且工作正常。感谢calcualtions) – Daler

+0

嘿,我在我的答案包括match_parent,谢谢。也许你应该在你的答案上发布最终的布局,以便其他人可以看到。 – Budius

0

,如果你把

android:layout_weight="1" 

和你weightSum是10,则 将采取90%的默认值。 如果你想在任何布局上只显示你的按钮10%,那么你应该给9的权重属性(如果权重为10)。

请参考here关于WeightSum和Weight Attributes的更多解释。希望它会有所帮助。

+0

不,它会占用90%的layout.be现在清楚。 –

+0

它不工作,我错过了什么地方。如果你可以出来更具体什么放在哪里,将是伟大的 – Daler