Android之include避免代码重复
来源:互联网 发布:外卖配送软件 编辑:程序博客网 时间:2024/05/21 22:46
在做布局时,经常有些部分是重复的,比如title或者foot的地方,最简单的办法当然是直接复制过去,
这里介绍include的用法,有过c++或者c经验的同学一看就明白了,就是把另一个布局包含进来.
先看下实现的效果:
里面上下各有两个文字布局,是用include包含进去的,直接看代码
activity_main.xml:
<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" > <include android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="30dp" layout="@layout/header" /> <ViewStub android:id="@+id/pic_stub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:inflatedId="@+id/pic_view_id_after_inflate" android:layout="@layout/pic_view" /> <include layout="@layout/footer" /></RelativeLayout>
很清楚的两个include,至于中间的viewstub,在下一篇博客里讲解
地址:http://blog.csdn.net/jason0539/article/details/26132267
代码里我用了两个include,
第一个比较复杂,设置了属性,让它在布局顶部
第二个比较简洁,属性都没有设置,但是位置却很正确,在布局的底部,
这样的目的是为了展示include属性的两种设置方式,可以在include标签里设置,也可以在所要引用的布局里面设置
所包含的两个布局文件如下:
header.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="0dp" android:gravity="center_horizontal" android:text="Header Without Properties!" ></TextView>
在这里面的宽度高度都设置为0了,目的就是要在布局中通过include标签设置它的布局
footer.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:gravity="center_horizontal" android:text="With Properties!" />
这个的属性设置的很完整,所以在布局中include标签里就什么都没设置,直接引用就行了,
两种方法效果相同,看个人喜好来用.
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539(转载请说明出处)
2 2
- Android之include避免代码重复
- Android之include避免代码重复
- 打造高质量Android代码系列之避免重复代码
- 50个Android开发技巧(2,使用延迟加载以及避免代码重复,include和viewstub)
- 通过 include 标签来避免代码重复加载
- 如何避免代码重复
- 利用<include />标签来避免重复渲染
- Android 避免重复提交
- 使用#include消除重复代码
- Android:利用Style和include标签来减少布局中的重复代码
- JavaScript:避免代码的重复执行
- 代码生成 避免重复劳动 省时省力
- 使用延迟加载以及避免代码重复
- android中从手机添加联系人,如何避免重复添加的代码的问题
- android 开发技巧(2)--使用延迟加载以及避免代码重复
- Android动态setId,避免id重复
- android 避免线程的重复创建
- 浅谈Android界面绘制避免重复渲染
- 资料库保存树结构的三种方法
- 修改字段排列顺序
- hdu 1317 XYZZY(spfa判环)
- MyEclipse中快捷键
- 比较两个List的算法 java实现
- Android之include避免代码重复
- 我说
- 2431Expedition POJ- 优先队列 + 贪心
- hdu 2844 Coins
- Rhino学习
- 还有就是演习地点正好在东海的一些比较敏感的区域
- linux vim设置字体大小
- POJ 3069 Saruman's Army
- mysql 启动错误-server PID file could not be found