问题
My main.xml layout simply contains a button and a LinearLayout
content_area, which shows below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/myBtns"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_one"
android:layout_width="100dip"
android:layout_height="30dip"
android:text="button one"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!--inflate layout here-->
</LinearLayout>
</LinearLayout>
I have another layout (content_one.xml) which is going to be used to embed(inflate) to content_area
(id value) of the main.xml layout:
content_one.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView.../>
...
<Button .../>
</LinearLayout>
I tried to use LayoutInflater
to embed content_one.xml into content_area
of main.xml:
public class MyActivity extends Activity{
private LinearLayout contentArea;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contentArea= (LinearLayout) findViewById(R.id.content_area);
LayoutInflater inflater = (LayoutInflater)MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = (View) inflater.inflate(R.layout.content_one, null);
contentArea.addView(inflatedView);
}
But it does not work, content_one.xml does not show in content_area
of main.xml . I got the following error message from LogCat in eclipse
(Please mouse right click on the following image and view image)
why my LayoutInflater is not working? Where am I wrong?
回答1:
The View
inflates correctly, but you have a fill_parent
height on the myBtns
layout, so the other LinearLayout is not visible. If you change it to wrap_content
you can see the other LinearLayout.
来源:https://stackoverflow.com/questions/6665988/why-layoutinflater-does-not-work-in-my-case