我的 main.xml中布局只包含一个按钮和一个的LinearLayout content_area ,这下图为:
My main.xml layout simply contains a button and a LinearLayout content_area, which shows below:
<LinearLayout xmlns:android="schemas.android/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="schemas.android/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 的的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不会在我的情况下工作吗?
发布评论