2012-11-10 38 views
13

Khi sử dụng fill_parent, maxWidth không có hiệu lực.maxWidth không hoạt động với 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> 

Trả lời

23

Thuộc tính maxWidth không ảnh hưởng đến chiều rộng match_parent (hoặc bị phản đối fill_parent), họ loại trừ lẫn nhau. Bạn cần sử dụng wrap_content.

Cũng bất kỳ bố trí mà chỉ có một đứa trẻ có thể có thể được gỡ bỏ, ví dụ bạn có thể đơn giản hóa bạn bố trí hiện tại để:

<?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

Cảm ơn bạn! Làm thế nào để điền vào EditText của tôi tất cả chiều rộng của màn hình nhưng không nore 500dip? Ví dụ một số màn hình có độ phân giải <500. –

+11

Điều này có vẻ ngớ ngẩn nhưng nó hoạt động: thêm 'minWidth' và đặt cả" min "và" max "thành' 500dp'. – Sam

+1

@Sam - điều này dường như không hoạt động đối với tôi, editText vẫn lấp đầy toàn bộ màn hình ngoại trừ lề. Bạn có thể xây dựng? – katzenhut

3

Bạn có thể đạt được hiệu quả tương tự sử dụng bố trí khác nhau cho màn hình khác nhau. Giả sử bạn muốn EditText để lấp đầy chiều rộng có sẵn nhưng không quá 480dp. Difine bố trí của bạn như sau:

bố trí/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"/> 

bố trí w480dp/my_layout.xml: (chọn bạn chiều rộng tối đa là vòng loại ở đây)

<?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"/> 
Các vấn đề liên quan