【问题标题】:Android Layout Inflater not working?Android Layout Inflater 不工作?
【发布时间】:2014-09-06 16:03:42
【问题描述】:

我的主要 java 类中有一个充气器,主要是因为我希望能够在布局中的“findViewById”,而不是在“setContentView”(activity_main.xml)中设置的布局。

由于我是 Android 开发人员和 Java 的新手,所以我认为我可以使用布局充气器来做到这一点。无论如何,我正在尝试扩展名为“box_middle”的布局。

这是我创建充气机的代码(并尝试使用它)。

LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.box_middle, null);

    drawerGrid = (GridView) view.findViewById(R.id.content);

我正在像这样初始化“drawerGrid”(公共)

 GridView drawerGrid;

最终结果是什么都没有显示在 XML 代码中的 GridView 所在的位置。

这里是 XML 代码 (box_middle)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Card visible layout -->
<LinearLayout
    android:id="@+id/card_main_layout"
    style="@style/card.main_layout"
    android:orientation="vertical"
    android:layout_width="142dip"
    android:layout_height="150dip"
    android:background="#ffff0c00">

    <GridView
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

</LinearLayout>

<!-- Compound view for Shadow
       If you want to customize this element use attr card:card_shadow_layout_resourceID -->
<it.gmariotti.cardslib.library.view.component.CardShadowView
    style="@style/card.shadow_outer_layout"
    android:id="@+id/card_shadow_layout"
    android:layout_width="150dip"
    android:layout_height="wrap_content"/>

</LinearLayout>

谢谢。

顺便说一句,我正在使用 Android Studio 和 this 库。

【问题讨论】:

    标签: java xml android-layout layout-inflater


    【解决方案1】:

    你使用

    LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.box_middle, null);
    

    使视图膨胀,但不要将其附加到窗口。不要忘记使用this.setContentView(view);

    所有的代码应该是:

    LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.box_middle, null);
    this.setContentView(view);`
    

    【讨论】:

    • 这可行,但结果是视图切换到视图“box_middle”,它意味着在视图“activity_main”上。不过感谢您的回答。
    • 您可以将视图“box_middle”的可见性设置为gone。但是,如果她不依附在窗户上,您将找不到视野。 :)
    【解决方案2】:

    如果你的主类扩展了 Activity,你也可以使用 findViewById...

    这样(但请确保在您必须 setContentView' (activity_main.xml) 之前)

    drawerGrid = (GridView) findViewById(R.id.content);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-23
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多