【发布时间】: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