Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何动态地包含XML布局?_Android_Layout_Android Layout_Include - Fatal编程技术网

Android:如何动态地包含XML布局?

Android:如何动态地包含XML布局?,android,layout,android-layout,include,Android,Layout,Android Layout,Include,我想将UI分解为几个XML布局。第一个是主布局,另一个是内容布局 我希望能够在运行时动态设置应该包括哪些内容布局,因此我不希望在XML文件中设置“layout=“@+layout/content\u layout” 以下是我的布局: main_layout.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an

我想将UI分解为几个XML布局。第一个是主布局,另一个是内容布局

我希望能够在运行时动态设置应该包括哪些内容布局,因此我不希望在XML文件中设置
“layout=“@+layout/content\u layout”

以下是我的布局:

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>
<?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_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>
<?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" />

我该怎么做

谢谢!

您可以尝试使用,只需通过编程更改要充气的布局即可


是的,我通常就是这样做的,但显然,ViewStub应该用于不经常使用的元素。如果您只在两种不同类型的列表视图之间进行更改,您可以通过编程方式更改列表视图绑定的适配器(因此使用不同的列表视图项XMLs)。不幸的是,我有几个内容布局,其中一些更复杂。我在另一个案例中尝试使用addView,但看起来两个布局都是相互叠加的。第二个孩子的意思是在第二个级别,所以在我的RelativeLayout下,对吗?我还可以使用android:layout_belowe=“@+id/title”这样的引用吗“在我的内容布局中,如果它们未在同一XML文件中定义,是否会发生事件?”?如果没有,他们如何知道如何将其插入正确的位置?ThanksIt将添加到您放置推荐的位置(//我想在这里包含我的内容??)。对于复杂布局,始终可以通过getChildCount()、getChildAt()循环查看(分组)childs,并在需要的位置添加视图。此外,您可以在代码中通过view.setId()设置id,然后引用该id。当然,这是代码中的常量,而不是通过R.id.someId,因为通过java代码分配的id不在R文件中。好的,我知道了,索引1(第二个子项)实际上意味着它将插入标题和按钮之间,0将位于标题之前,等等。。。谢谢。@MathiasLin:你能帮我一下吗:我想现在方法输入的顺序颠倒了:,int),所以它应该是
rl.addView(layoutInflater.inflate(R.layout.content\u layout,this,false),1)