2015-09-24 39 views
0

我有這個簡單的佈局問題:的Android的LinearLayout重量怪事

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="10" 
      android:ems="10" 
      android:hint="Title" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/editText2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="45" 
      android:ems="10" 
      android:hint="First Name" /> 

     <EditText 
      android:id="@+id/editText3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="45" 
      android:ems="10" 
      android:hint="Last Name" /> 

    </LinearLayout> 

正如你可以看到這個應該分裂屏幕爲10-45-45,但佈局看起來是這樣的:

enter image description here

這似乎是隻有EditTexts的問題。

+1

使用時的體重,你應該設置的意見寬度0dp。此外,android:ems可能會干擾體重。 – michaelcarrano

回答

1

在這裏你去 -

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:ems="10" 
     android:hint="Title" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="45" 
     android:ems="10" 
     android:hint="First Name" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="45" 
     android:ems="10" 
     android:hint="Last Name" /> 

</LinearLayout> 

我設置android:layout_width到0dp。當您在使用重量時使用水平方向的LinearLayout時,需要將android:layout_width設置爲0dp,並且當方向爲垂直時,需要將android:layout_height設置爲0dp,這樣權重才能生效。希望能幫助到你。

+0

謝謝,從來沒有想過會解決這個問題。 :) – breakline

1

只需使用

android:layout_width="0dp" 

,而不是

android:layout_width="wrap_content"