2012-07-20 53 views
1

我想通过layoutInflater将布局放入另一个布局。addView FILL_PARENT不工作

这里的main_layout.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/ads_image_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/ic_launcher" /> 

<RelativeLayout 
    android:id="@+id/host_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/navigation_buttons_layout" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ads_image_view" > 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/navigation_buttons_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" > 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

</RelativeLayout> 

和这里的vitrin_layout.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ff0000" 
    android:text="smallred" /> 

终于onCreate方法为我的主要活动:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 
    ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout); 
    View view = LayoutInflater.from(getBaseContext()).inflate(
      R.layout.vitrin_layout, null); 
    parent.addView(view, LayoutParams.FILL_PARENT); 

    setTitle(getString(R.string.title_activity_vitrin)); 
} 

问题是,vitrin_layout,不适合其父(host_layout);尽管我已经设置了fill_parent,我可以!
我不明白为什么。

编辑:我真的很抱歉,但我贴了错误的vitrin_layout文件,我修好了。

回答

9

使用此addView方法。顺便说一句,不要使用getBaseContext(),除非你知道你为什么使用它,而是使用活动的上下文(this)。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_layout); 
    ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout); 
    View view = LayoutInflater.from(this).inflate(R.layout.vitrin_layout, null); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     ViewGroup.LayoutParams.FILL_PARENT, 
     ViewGroup.LayoutParams.FILL_PARENT); 
    parent.addView(view, params); 

    setTitle(getString(R.string.title_activity_vitrin)); 
} 
+0

我在这个问题上犯了一个错误,请再次阅读 – mehrmoudi 2012-07-20 07:39:22

+0

无论如何,你仍然在不经意地使用addView(),请参阅addView()方法的文档。 – biegleux 2012-07-20 07:44:41

2
parent.addView(view, LayoutParams.FILL_PARENT); 

这种方法是ViewGroup.addView(查看孩子,INT指数)实际调用。 您需要调用ViewGroup.addView(View child,ViewGroup.LayoutParams params)方法来设置LayoutParams。

+0

我在这个问题上犯了一个错误,请再读一遍 – mehrmoudi 2012-07-20 07:40:30