Android 如何在<;中添加新布局;包括>;程序化

Android 如何在<;中添加新布局;包括>;程序化,android,view,include,Android,View,Include,单击按钮时,我需要将gmt\u选项\u列表\u关闭.xml并在标记中附加一个新布局gmt\u选项\u列表\u打开.xml parentOfAllInGMT.addView(layoutInflater.inflate(R.layout.gmt_option_list_dst_on, parentOfAllInGMT, true), 0); 以下是xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:

单击按钮时,我需要将
gmt\u选项\u列表\u关闭.xml
并在
标记中附加一个新布局
gmt\u选项\u列表\u打开.xml

parentOfAllInGMT.addView(layoutInflater.inflate(R.layout.gmt_option_list_dst_on, parentOfAllInGMT, true), 0);
以下是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="6" >

    <include
        android:id="@+id/gmt_optionlist"
        android:layout_weight="5"
        layout="@layout/gmt_option_list_dst_off" />

    <Button
        android:id="@+id/dstbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="DST Off" >
    </Button>

</LinearLayout>
parentOfAllInGMT.addView(layoutInflater.inflate(R.layout.gmt_option_list_dst_on, parentOfAllInGMT, true), 0);

但这是不对的

试试这个,它应该可以做到:

parentOfAllInGMT.addView(layoutInflater.inflate(R.layout.gmt_option_list_dst_on, parentOfAllInGMT, true), 0);
ViewStub stub = (ViewStub) findViewById(R.id. gmt_optionlist);
stub.inflate();

希望这能有所帮助。

使用addView()和removeViewAt调用包含include标记的LinearLayout

我建议您使用类似以下内容:
parentOfAllInGMT.addView(layoutInflater.inflate(R.layout.gmt_option_list_dst_on, parentOfAllInGMT, true), 0);