2011-10-13 75 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