【问题标题】:Android: How to dynamically include a XML Layout?Android:如何动态包含 XML 布局?
【发布时间】:2011-05-02 02:11:32
【问题描述】:

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

我希望能够设置在运行时动态包含哪个 content_layout,所以我不想在我的 XML 文件中设置 "layout="@+layout/content_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>

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

我该怎么做?

谢谢!

【问题讨论】:

    标签: android layout android-layout include


    【解决方案1】:
    <RelativeLayout android:id="@+id/rl" ...
    

    在您的代码中:

    // 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) ); 
    

    【讨论】:

    • 我在另一种情况下尝试使用 addView,但它看起来像两个布局相互叠加。第二个孩子的意思是在第二级,所以在我的 RelativeLayout 下对吧?如果它们没有在同一个 XML 文件中定义,我还能在我的内容布局中使用像 android:layout_belowe="@+id/title" 这样的引用吗?如果没有,他们怎么知道如何将它插入正确的位置?谢谢
    • 它会被添加到你放置推荐的地方(//我想在这里包含我的内容????)。对于复杂的布局,您始终可以通过 getChildCount()、getChildAt() 遍历视图(组)子项,然后将视图添加到您想要的位置。此外,您可以通过代码中的 view.setId() 设置一个 id,然后引用它。但当然,这将是代码中的常量,而不是通过 R.id.someId,因为通过 java 代码分配的 id 不在您的 R 文件中。
    • 好的,我明白了,索引1(第二个孩子)实际上意味着它会插入到标题和按钮之间,0会在标题之前,等等......谢谢。
    • @MathiasLin :请你帮帮我:stackoverflow.com/questions/8818042/…
    • 我认为方法输入的顺序现在颠倒了:developer.android.com/reference/android/view/…, int) 所以应该是rl.addView(layoutInflater.inflate(R.layout.content_layout, this, false), 1 );
    【解决方案2】:

    您可以尝试使用 ViewStub 并更改它将以编程方式膨胀的布局。

    This answer discusses using one ViewStub.

    【讨论】:

    • 是的,这就是我通常的做法,但显然,ViewStubs 应该用于您不经常使用的元素。
    • 如果您只是在两种不同类型的列表视图之间进行更改,您可以通过编程方式更改列表视图绑定的适配器(因此使用不同的列表视图项 XML)。
    • 不幸的是,我有几个内容布局,其中一些比这更复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2015-05-24
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    相关资源
    最近更新 更多