2014-03-28 62 views
0

我有一個線性佈局和裏面的一個按鈕。我希望按鈕適合佈局寬度的大約90%。以下一些教程中,我使用的屬性layout_weight,這就是我的代碼:Android的重量和佈局寬度

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FFFFFF" 
    android:weightSum="5" > 

<Button 
    android:id="@+id/newgame" 

    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 

    android:text="@string/newgame" 
    android:textSize="16sp" 
    android:typeface="monospace" 
    android:textStyle="bold" 
    android:background="@drawable/backrepeat" /> 

</LinearLayout> 

現在,我的按鈕消失,如果我設置layout_width到0dp像極了教程說...可能是什麼問題呢? 在此先感謝。

+1

改變方向的LinearLayout水平或添加重量爲1的不可見視圖。 – joao2fast4u

+0

即使您使用重量,如果您使用正確的內容,您也只想知道方向t ...方向和價值... –

回答

0

試試這個,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
> 

<Button 
    android:id="@+id/newgame" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="9" 

    android:text="@string/newgame" 
    android:textSize="16sp" 
    android:typeface="monospace" 
    android:textStyle="bold" 
    android:background="@drawable/backrepeat" /> 

</LinearLayout> 

默認情況下,線性佈局的方向是水平的,因此現在將調整它考慮到橫向寬度。 :)

+0

謝謝!我只需將android:weightSum =「10」添加到LinearLayout以使其工作:) – esseara

+0

歡迎您。 :)很高興聽到它的工作。 –

2

線性佈局權重只在佈局方向的維度上分配空間。你有一個垂直線性佈局和一個0px寬的元素 - 重量機制不會添加任何東西。將線性佈局方向更改爲水平方向。

1

Vertical orientation!

Vertical

您可以輕鬆地設置Weight你只是用這樣的.... 相反的0dp必須使用0dip

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#FFFFFF" 
android:orientation="vertical" 
android:weightSum="5" > 

<Button 
    android:id="@+id/newgame" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 
    android:background="@drawable/backrepeat" 
    android:text="@string/newgame" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:typeface="monospace" /> 

</LinearLayout>