2011-10-13 31 views
5

Tôi có bố trí và tôi muốn thổi phồng rằngInflater thổi phồng nhưng chiều cao là nhỏ (trông giống như wrap_content và cần fill_parent)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    >  
    </EditText> 

</LinearLayout> 

như

LinearLayout ll=(LinearLayout)findViewById(R.id.llContainer); 
    View view; 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = inflater.inflate(R.layout.question_free_text, null); 
    ll.addView(view); 

nơi ll là

<LinearLayout 
    android:id="@+id/llContainer" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="20dp" 
    android:layout_marginBottom="20dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
> 
</LinearLayout> 

trong xml khác, nhưng vấn đề là khi nó thổi phồng nó cho thấy nhưng chiều cao là lớn (fill_parent, nó trông giống như wrap_content, nhưng không có wrap_content trong bố trí). Ai có thể giúp tôi ?

+0

có thể là vì những 2tags .. android: layout_marginTop = "20dp" android: layout_marginBottom = "20dp" – ngesh

+2

thay thế null bằng chế độ xem gốc, trong khi tăng. –

Trả lời

14

Như Yashwanth Kumar nói một cách chính xác trong các ý kiến, tham số thứ hai của thổi phồng-phương pháp nên xem gốc, trong đó quan điểm mới sẽ được chèn vào:

LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer); 
View view; 
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.question_free_text, ll); 

Nếu xem gốc được cung cấp trong thổi phồng cuộc gọi, LayoutInflator gọi phương thức generateLayoutParams(ViewGroup.LayoutParams p) của chế độ xem gốc này để nhận một số LayoutParams (về cơ bản chứa thông tin về mức độ lớn của một chế độ xem/có thể) sẽ được chuyển đến chế độ xem mới.

Lưu ý rằng nếu bạn cung cấp chế độ xem gốc, chế độ xem tăng cao sẽ tự động được thêm vào chế độ xem gốc qua root.addView(View child, LayoutParams params).

Bạn cũng có thể chuyển thông số thứ ba sang phương pháp thổi phồng (boolean attachToRoot), nếu giá trị này là false, chế độ xem mới sẽ không được thêm tự động vào chế độ xem gốc, nhưng LayoutParams vẫn được đặt bằng setLayoutParams(params). Bạn có thể sử dụng điều này nếu bạn cây đũa phép để thêm bạn xem thủ công vào xem gốc (ví dụ như ở một vị trí/index cụ thể):

LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer); 
View view; 
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.question_free_text, ll, false); // the LayoutParams of view are set here 
ll.addView(view, 2); 
+1

câu trả lời hay, câu trả lời đúng. – VinceStyling

Các vấn đề liên quan