Android 在我的第一个布局资源中包含第二个布局资源

Android 在我的第一个布局资源中包含第二个布局资源,android,include,android-layout,Android,Include,Android Layout,是否有一种方法可以将一种资源包含在另一种资源中,例如多个活动布局中的标题设计。我知道我可以在运行时添加它,可以在XML中完成吗?当然可以。有关详细说明,请查看帖子: 是的,您可以在XML中这样做。见 关于合并/包含 基本上,您将有1个layoutroot.xml,如下所示: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/

是否有一种方法可以将一种资源包含在另一种资源中,例如多个活动布局中的标题设计。我知道我可以在运行时添加它,可以在XML中完成吗?

当然可以。有关详细说明,请查看帖子:


是的,您可以在XML中这样做。见 关于合并/包含

基本上,您将有1个layoutroot.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rootLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:id="@+id/headingLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
      <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/heading_layout" />
  </LinearLayout>
<RelativeLayout>
因此,在活动中,您可以设置ContentViewr.layout.root;其中包括标题

除此之外,您还可以通过编程方式执行一些很酷的操作,例如在setContentView;之后将布局从xml插入到root.xml中:

其中rootLayout是由id找到的父相对论,R.layout.home是要添加到根目录的布局

<?xml version="1.0" encoding="utf-8"?>
<merge
  xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
        <ImageView
          android:id="@+id/titleImg"
          android:src="@drawable/bg_cell"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" />
        <TextView
          android:id="@+id/titleTxt"
          android:layout_centerInParent="true"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
    </RelativeLayout>
</merge>
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.home, rootLayout);