【问题标题】:Why LayoutInflater does not work in my case?为什么 LayoutInflater 在我的情况下不起作用?
【发布时间】:2011-07-12 14:39:22
【问题描述】:

我的 ma​​in.xml 布局只包含一个按钮和一个LinearLayout content_area,如下所示:

<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) 将用于嵌入(膨胀)到 ma​​in.xmlcontent_area(id 值)布局:

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>

我尝试使用LayoutInflatercontent_one.xml嵌入到ma​​in.xmlcontent_area中:

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 没有显示在 ma​​in.xmlcontent_area 中。我从 Eclipse 中的 LogCat 收到以下 错误消息

(请鼠标右击下图查看图片)

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

【问题讨论】:

  • 该日志不完整...粘贴整个日志(当然是发生错误的地方)...并且文本不是图像。
  • @Cristian,我不知道如何从 eclipse LogCat 控制台复制整个错误消息,但我上传了另一张替换旧图像的图像

标签: android android-layout android-emulator android-widget android-manifest


【解决方案1】:

View 正确膨胀,但您在 myBtns 布局上有一个 fill_parent 高度,因此其他 LinearLayout 不可见。改成wrap_content就可以看到另一个LinearLayout了。

【讨论】:

  • 对!正因为如此。谢谢。
猜你喜欢
  • 2018-07-15
  • 2021-09-29
  • 2017-02-22
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多