2012-05-11 32 views
3

也許我誤解了layout_weight參數,但爲什麼這樣的事情不工作,我不能得到它...的Android Layout_weight

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" android:weightSum="1"> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.2" 
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:text="TextView" 
    android:background="@color/apr_yellow"/> 

</LinearLayout> 

我有一個的LinearLayout內的三個或更多textviews。 我想將它們放在具有百分比寬度的列中。 而這正是它產生:

隨着layout_weight設置好的〜0.3/0.2/0.5

Android layout_weight incorrectly

然後我而變化的layout_weight參數只是有點如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" android:weightSum="1"> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" 
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.4" 
    android:text="TextView" 
    android:background="@color/apr_yellow"/> 

</LinearLayout> 

而現在我得到它,因爲我想:

隨着layout_weight設置爲0.3/0.2/0.5

Android layout_weight correctly

是一個bug或者也許我真的誤會約layout_weight整個事情?

+0

嘗試改變機器人:weightSum = 「1」 到機器人:weightSum = 「1.0」 重量之和應的浮點值。 – Tarun

回答

12

當您使用android:layout_weight時,應該設置android:layout_width="0dp"(如果垂直方向 - android:layout_height="0dp")。

+0

+1 corret ......... – MAC

+0

謝謝你,它的工作。 – Chris

+0

歡迎您 –

1

enter image description here

嘗試此....

 <TextView 
      android:background="#F2e" 
      android:layout_weight="3" 
      android:id="@+id/textView1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="TextView" /> 

     <TextView 
      android:background="#F6e" 
      android:layout_weight="2" 
      android:id="@+id/textView2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="TextView" /> 

     <TextView 
      android:background="#F45" 
      android:layout_weight="5" 
      android:id="@+id/textView3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="TextView" /> 

    </LinearLayout> 
+0

謝謝您的幫助。 – Chris