我想通過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