[英]Android Layout Inflater not working?
I have an inflater in my main java class, mainly because I want to be able to 'findViewById' in a layout other then the one set in 'setContentView' (activity_main.xml). 我的主要Java类中有一个充气机,主要是因为我希望能够在“ setContentView”(activity_main.xml)中的布局以外的布局中“ findViewById”。
Since I'm new to Android dev and Java in general, I thought I could do this with a layout inflater. 由于我是Android开发人员和Java的新手,所以我认为我可以使用布局填充器来做到这一点。 Anyway, the layout I'm trying to inflate a layout named "box_middle".
无论如何,我正在尝试为名为“ box_middle”的布局充气。
This is my code where I create the inflater (and try to use it). 这是我创建充气机(并尝试使用它)的代码。
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);
I am initialising "drawerGrid" like this (public) 我正在像这样初始化“ drawerGrid”(公共)
GridView drawerGrid;
The end result is nothing shows up where the GridView in the XML code is. 最终结果是什么都没有显示在XML代码中GridView的位置。
Here is the XML code (box_middle) 这是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>
Thanks. 谢谢。
BTW I'm using Android Studio and this library. 顺便说一句,我正在使用Android Studio和该库。
You use 你用
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.box_middle, null);
to inflate the view, but you don't attach it to the window. 使视图膨胀,但不要将其附加到窗口。 Don't forget to use
this.setContentView(view);
不要忘记使用
this.setContentView(view);
All the code should be : 所有代码应为:
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.box_middle, null);
this.setContentView(view);`
If your main class extends Activity you can also use findViewById... 如果您的主类扩展了Activity,则还可以使用findViewById ...
like that (But be sure that before you must setContentView' (activity_main.xml) ) 那样(但是请确保在必须设置ContentView'(activity_main.xml)之前)
drawerGrid = (GridView) findViewById(R.id.content); 抽屉式网格=(GridView)findViewById(R.id.content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.