Android 包括来自其他布局的对象

Android 包括来自其他布局的对象,android,Android,我得到了如下xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:

我得到了如下xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabBar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#474747"
  android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/BtnSlide"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@null"
        android:src="@drawable/lin" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="SteriaFIT Mobile"
        android:gravity="center"
        android:paddingLeft="12dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
 <include  layout="@layout/tab_bar" android:id="@+id/tabBar" />
但在我看来,这行代码将包括整个布局文件

编辑

应该提到的是,此方法还将为我提供id为
button1

编辑2

好的,将
选项卡栏
放入一个新的布局文件后,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabBar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#474747"
  android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/BtnSlide"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@null"
        android:src="@drawable/lin" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="SteriaFIT Mobile"
        android:gravity="center"
        android:paddingLeft="12dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
 <include  layout="@layout/tab_bar" android:id="@+id/tabBar" />

有人能给我一个提示吗?=

好的,如果提供的布局文件名是horz_scroll_应用程序,它将包括整个。
签出以获取有关包含的清晰解释。

include用于将整个xml文件包含到当前布局中。如果只需要tabBar路径,请将它们放入单独的布局文件中,然后将它们也包括在原始视图中

您将正常访问该视图:

LinearLayout tabBar = (LinearLayout) findViewById(R.id.tabBar);
ImageButton btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);

我相信,将视图放在一起的整个过程都是在编译时完成的。因此,您可以像对待xml布局中的视图一样对待它。

这实际上是我阅读的文章,以了解如何通过id仅获取对象。您发布了正确的xml布局吗?xml布局中不能有多个根元素。我已经发布了正确的xml。我可以在一个布局中有更多的布局,不是吗?您可以在xml中有更多的布局,但其中只有一个必须是根元素。
LinearLayout tabBar = (LinearLayout) findViewById(R.id.tabBar);
ImageButton btnSlide = (ImageButton) tabBar.findViewById(R.id.BtnSlide);