为什么LayoutInflater不会在我的情况下工作吗? [英] Why LayoutInflater does not work in my case?

查看:196
本文介绍了为什么LayoutInflater不会在我的情况下工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 main.xml中布局只包含一个按钮和一个的LinearLayout content_area ,这下图为:

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>

我有另一个布局( content_one.xml ),这是怎么回事的情况下使用嵌入(膨胀),以 content_area (id值)在 main.xml中布局:

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>

我试图用 LayoutInflater 嵌入 content_one.xml content_area 的< STRONG>的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);
}

但它不工作, content_one.xml content_area 不显示的 main.xml中。我在Eclipse中得到了下面的错误信息从LogCat中

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

请鼠标右键单击下面的图像和查看图像

为什么我的LayoutInflater不工作?我在哪里错了?

why my LayoutInflater is not working? Where am I wrong?

推荐答案

查看正常膨胀,但你有一个 FILL_PARENT myBtns 布局高度,所以其它的LinearLayout是不可见的。如果您将其更改为 WRAP_CONTENT 你可以看到其他的LinearLayout。

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.

这篇关于为什么LayoutInflater不会在我的情况下工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆