include 用法

 

一、include 用法

 titlebar.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="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:background="#ff00ee">

    <View
        android:id="@+id/title_view"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp">

        <TextView
            android:id="@+id/title_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="测试"
            />

        <TextView
            android:id="@+id/add_tv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:paddingRight="15dp"
            android:gravity="center"
            android:text="添加"/>
    </RelativeLayout>
</LinearLayout>

应用include,code 如下: 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#Fff00ee">
    <include
        android:id="@+id/title"
        layout="@layout/titlebar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:visibility="gone"
        tools:visibility="visible"/>
</RelativeLayout>
include标签使用注意点:
1,在使用<include>标签时,可以重写所有layout属性的,include中指定的layout属性将会覆盖掉titlebar中指定的layout属性。
而非layout属性(如android:visibility="gone")则无法在<include>标签当中进行覆写。另外需要注意的是,如果想要在<include>标签当中覆写layout属性,必须要将layout_width和layout_height这两个属性也进行覆写,否则覆写效果将不会生效。
2,当一个xml布局文件中包含多个include标签的时候,需要为每个include设置ID,才能找到相应子View的控件,否则只能找到第一个include的layout布局,以及该布局的控件。
3,如果给include所加载的layout布局的根容器设置了id属性,也在include标签中设置了id属性,同时需要在代码中获取根容器的控件对象时,最好将这两个id设置相同的名称!否则,可能获取不到根容器对象,即为null。

二、merge 用法

https://www.cnblogs.com/leipDao/p/8981687.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值