Android中<include>标签的使用及注意事项

前言

  <include>标签可以实现在一个layout中引用另一个layout的布局,这通常适合于界面布局复杂、不同界面有共用布局的APP中,比如一个APP的顶部布局、侧边栏布局、底部Tab栏布局、ListView和GridView每一项的布局等,将这些同一个APP中有多个界面用到的布局抽取出来再通过<include>标签引用,既可以降低layout的复杂度,又可以做到布局重用(布局有改动时只需要修改一个地方就可以了)。

使用方法

  <include>标签的使用很简单,只需要在布局文件中需要引用其它布局的地方,使用layout="@layout/child_layout"就可以了:

<include layout="@layout/titlebar" />

  比如,include_voice_ctrl_bar_layout.xml是一个可以提取出来的共用布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <!-- 播放、暂停 -->
    <Button
        android:id="@+id/voiceBtnId"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:background="@drawable/voice_btn_selector"
        android:gravity="center"
        android:onClick="onClick"
        android:text="@string/voice_stop"
        android:textColor="@android:color/white"
        android:textSize="24sp" />
    <!-- 分割线 -->
    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/black" />
    <!-- 听录音 -->
    <Button
        android:id="@+id/listenBtnId"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:background="@drawable/listen_btn_selector"
        android:gravity="center"
        android:onClick="onClick"
        android:text="@string/voice_listen"
        android:textColor="@android:color/white"
        android:textSize="24sp" />
    <!-- 分割线 -->
    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/black" />
    <!-- 下一个 -->
    <Button
        android:id="@+id/nextBtnId"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:background="@drawable/next_btn_selector"
        android:gravity="center"
        android:onClick="onClick"
        android:text="@string/voice_next"
        android:textColor="@android:color/white"
        android:textSize="24sp" />
</LinearLayout>

  在需要使用该共用布局的地方作如下调用即可:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_bottom_layer">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:background="@drawable/background_top_layer">
        <include layout="@layout/include_voice_text_bar_layout" />
        <include layout="@layout/include_voice_ctrl_bar_layout" />
    </RelativeLayout>
</RelativeLayout>

注意事项

  • include和其它组件标签(RelativeLayout、LinearLayout、TextView等)一样,都可以使用layout属性来设置布局文件的宽高和位置,但需要注意的是:必须要复写android:layout_width和android:layout_height属性才能使用其它属性(比如:android:layout_grivity、android:layout_align...、android:id等),这样可以避免include引用layout中的子组件属性影响到include的布局效果:
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_bottom_layer">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="@drawable/background_top_layer">
            <include layout="@layout/include_voice_text_bar_layout" />
            <include
                android:layout_width="match_parent"
                android:layout_height="61dp"
                android:layout_alignParentBottom="true"
                layout="@layout/include_voice_ctrl_bar_layout"
                />
        </RelativeLayout>
    </RelativeLayout>
  • 建议将给<include>标签调用布局设置宽高、位置、ID等工作放在调用布局的根标签中,这样可以避免给<include>标签设置属性不当造成的各种问题(之前遇到过给include标签设置android:id属性后,程序实例化子布局中组件失败的现象):

  应该这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottomBarLayoutId"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    。。。
</LinearLayout>
  • <include layout="@layout/include_voice_ctrl_bar_layout" />

  而不是这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    。。。
</LinearLayout>
  • <include
    android:id="@+id/bottomBarLayoutId"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:layout_alignParentBottom="true"
    layout="@layout/include_voice_ctrl_bar_layout"
    />

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,041评论 25 709
  • RelativeLayout 第一类:属性值为true可false android:layout_centerHr...
    兀兀沙弥阅读 8,128评论 0 15
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,969评论 22 665
  • 云层遮盖住前方 看不清路 雾茫茫 黑暗中遗失自己 迷失彷徨 车水马龙的灯火 擦肩而过的你我 迫切想要逃过 寻找乌托...
    天空里放梦阅读 2,507评论 8 5
  • 我是沿途的过客 只要一个人坐坐 可什么都不想说 因为说了就会错 这究竟是谁的国 安享岁月的蹉跎 留恋某一场经过 静...
    十四有玉阅读 923评论 0 2