2013-01-09 30 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