2011-10-13 76 views
5

我的佈局,我想誇大這打氣筒充氣,但高度小(貌似WRAP_CONTENT和需要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> 

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); 

其中,LL爲

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

在其他XML中,但問題是當它膨脹它顯示,但高度很大(fill_parent,它看起來像wrap_content,但沒有wrap_content佈局)。有誰能夠幫助我 ?

+0

可能是因爲這些2tags的..機器人:layout_marginTop =「20dp」 機器人: layout_marginBottom =「20dp」 – ngesh

+2

在膨脹期間用父視圖替換null。 –

回答

14

如Yashwanth庫馬爾評價正確地提到的,充氣法的第二個參數應該是其中新的視圖將被插入根視圖:

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); 

如果根視圖在所提供的inflate-call,LayoutInflator調用該根視圖的generateLayoutParams(ViewGroup.LayoutParams p)方法來獲取一些LayoutParams(基本上包含有關視圖可以/應該有多大的信息),這些信息將被傳遞到新視圖。

請注意,如果您提供根視圖,則充氣視圖將通過root.addView(View child, LayoutParams params)自動添加到根視圖。

也可以傳遞第三個參數的充氣法(boolean attachToRoot),如果該值是false,新視圖不會被自動添加到根視圖,但的LayoutParams仍設置有setLayoutParams(params)。如果你魔杖可添加您手動瀏覽到根視圖(例如,在一個特定的位置/索引),您可以使用此:

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

很好的答案,正確的答案。 – VinceStyling