2012-11-10 60 views
13

使用fill_parent時,maxWidth不起作用。maxWidth不適用於fill_parent

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 
     <EditText android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:maxWidth="50dip"/> 
    </LinearLayout>  
</LinearLayout> 

回答

23

屬性maxWidth對的match_parent寬度沒有影響(或棄用fill_parent),它們是相互排斥的。您需要使用wrap_content

只生一個孩子的任何佈局大概可以去掉,比如你可以簡化你當前的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxWidth="50dip"/> 
+2

謝謝!如何填寫我的EditText屏幕的所有寬度但不是500dip?例如,某個屏幕的分辨率<500. –

+11

這看起來很愚蠢,但它起作用:添加'minWidth'並將「min」和「max」都設置爲'500dp'。 – Sam

+1

@Sam - 這似乎不適用於我,EditText仍然填充整個屏幕除邊緣。你能詳細說明一下嗎? – katzenhut

3

您可以使用不同的佈局不同的屏幕達到同樣的效果。假設你想讓你的EditText填充可用寬度但不超過480dp。 Difine你的佈局如下:

佈局/ my_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

佈局w480dp/my_layout.xml:(選擇你最大寬度爲這裏的預選賽)

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="480dp" 
      android:layout_height="wrap_content"/>