编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/72614683
使用include引入布局的作用
提取重复的布局代码,方便进行复用
如何使用
比如我们想要在线性布局中创建三块需要复用的布局headview、centerview、buttomview(用Button简单代替)
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/headview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/centerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/buttomview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
代码写出来了,但其他布局如果也要使用centerview,就需要将布局拷贝过去,导致代码冗余,但使用include引入centerview就可以让其他布局进行复用
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/headview"
android:id="@+id/headview"/>
<include layout="@layout/centerview"
android:id="@+id/centerview"/>
<include layout="@layout/buttomview"
android:id="@+id/buttomview"/>
</LinearLayout>
于是,我们就采用如下方法引入headview、centerview、buttomview,但是我们会发现,这三个布局重叠到一起去了,并没有像我们想象的上下堆叠排列
include使用注意事项
1,必须同时重载layout_width和layout_height属性,其他的属性才会起作用,否则会被忽略掉。
2,include里面设置背景颜色是不生效的,必须在include的布局里面写,include里面设置位置及大小信息,然后直接在include的布局里面设置match_parent即可
于是include引入布局代码正确写法如下:
//(layout)activity_main
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/headview"
android:id="@+id/headview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<include layout="@layout/centerview"
android:id="@+id/centerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<include layout="@layout/buttomview"
android:id="@+id/buttomview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)headview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/headview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)centerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/centerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)buttomview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttomview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------