关于在Android中一个XML文件包含另外一个XML的方法

在Android界面设计中,有时候需要一个XML主界面需要包含很多的子界面,如果写在同一个XML文件,该文件结构过于复杂,难以维护,因此可以考虑把各个子界面写成独立的XML文件,然后包含到主界面xml文件中,方法如下:

1.直接在XML中包含,写法:

<LinearLayout ……………… 
      <include layout="@layout/foot" />
</LinearLayout>
上面的foot是layout文件夹里面的foot.xml


2.使用程序代码实现

       View view=getLayoutInflater().inflate(R.layout.root,null);
       LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout1);
       layout.addView(view);

或者 最方便的是

 RelativeLayout syset_main=(RelativeLayout)act.findViewById(R.id.syset_main);

Inflater.inflate(R.layout.roomcreate, syset_main, true);

其中syset_main是父XML,roomcreate是需要添加的子XML

原文地址:https://www.cnblogs.com/chang-bin/p/android.html