2013-10-18 68 views
0

我試圖在LinearLayout中分配權重。 雖然它與ButtonView-S的作品,它只是可以用於簡單查看相反:android:layout_weight for view的行爲與預期相反

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#FF0000" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:background="#00FF00" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:background="#0000FF" /> 

</LinearLayout> 

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#FF0000" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:background="#00FF00" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:background="#0000FF" /> 

</LinearLayout> 

enter image description here

這是deffinetly實現ButtonView的那使其正常工作。 但是應該在View中實現(以及如何)與LinearLayout行爲一致? 非常感謝。

+1

對於兩者都是相同的輸出,重新檢查。 –

+0

對我來說也是一樣的輸出。 –

+0

是的,確實如此 - 我的自定義視圖錯誤( –

回答

0

您可以嘗試通過「FrameLayout裏」,以取代「查看」我不認爲查看有望被用作支架

+0

可能是這樣。如果我想實現自定義視圖並擴展視圖,該怎麼辦?應該在視圖中實現以符合LinearLayout的行爲?我明白它是onMeasure()。但是怎麼樣?我已經這樣做了,但它不適用於權重。 –

0

通過同樣的問題感到沮喪之後,我結束了使用TextView與沒有文字。

我只在Eclipse的圖形佈局模式中看到過這個問題。在我的Galaxy S2上,該應用按預期工作。

玩弄原始「視圖」的layout_weight確實會產生非常奇怪的效果。

另一種解決方法是將Viewlayout_height"0dp"更改爲"1dp"。我不知道爲什麼會修復它,但我猜測一些與零有關的深奧魔法(即一個錯誤,也許是一個神奇的絛蟲)。