Android的:如何动态地包含XML布局? [英] Android: How to dynamically include a XML Layout?
本文介绍了Android的:如何动态地包含XML布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我分解成UI几种XML布局。第一个将是主要的布局,以及其他的人将是内容的布局。
我想可以设置哪些content_layout应该动态地在运行时被包含,所以我不希望设置一个布局=@ +布局/ content_layout
在我的XML文件中。
下面是我的布局:
main_layout.xml:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =600dp
机器人:layout_height =800DP>
<的TextView
机器人:ID =@ + ID /标题
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真/>
<有/> <! - 我想包括在这里我的内容 - >
<按钮
机器人:ID =@ + ID / cancelButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_centerHorizontal =真
机器人:文本=取消/>
< / RelativeLayout的>
content_layout.xml:
< XML版本=1.0编码=UTF-8&GT?;
< ListView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /无所谓
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT/>
content_layout2.xml:
< XML版本=1.0编码=UTF-8&GT?;
< ListView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / whatever2
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT/>
我怎么能这样做?
谢谢!
解决方案
< RelativeLayout的机器人:ID =@ + ID / RL......
在你的code:
//得到你的外部相对布局
RelativeLayout的RL =(RelativeLayout的)findById(R.id.rl);
//膨胀内容的布局和其添加到相对布局作为第二子
//添加为第二个孩子,所以通过指数1(0,1,...)
LayoutInflater layoutInflater =(LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1,layoutInflater.inflate(R.layout.content_layout,这一点,FALSE));
I want to decompose my UI into several XML Layouts. The first one would be the main layout, and the other ones would be the content layouts.
I would like to be able to set which content_layout should be included dynamically at run-time, so I don't want to set a "layout="@+layout/content_layout"
in my XML file.
Here are my layouts:
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="800dp" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<include /> <!-- I WANT TO INCLUDE MY CONTENT HERE -->
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
content_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/whatever"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
content_layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/whatever2"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
How can I do that?
Thanks!
解决方案
<RelativeLayout android:id="@+id/rl" ...
In your code:
// get your outer relative layout
RelativeLayout rl = (RelativeLayout) findById(R.id.rl);
// inflate content layout and add it to the relative layout as second child
// add as second child, therefore pass index 1 (0,1,...)
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
这篇关于Android的:如何动态地包含XML布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文