Why LayoutInflater does not work in my case?

可紊 提交于 2019-12-25 00:27:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!