发布时间:2025-12-09 22:25:23 浏览次数:36
一、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"><Viewandroid:id="@+id/title_view"android:layout_width="match_parent"android:layout_height="0dp" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="45dp"><TextViewandroid:id="@+id/title_txt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="测试"/><TextViewandroid: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"><includeandroid: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