我想通过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文件,我修好了。
我在这个问题上犯了一个错误,请再次阅读 – mehrmoudi 2012-07-20 07:39:22
无论如何,你仍然在不经意地使用addView(),请参阅addView()方法的文档。 – biegleux 2012-07-20 07:44:41