Android实战经验之Incude便签
来源:互联网 发布:mac safari关闭标签 编辑:程序博客网 时间:2024/05/16 09:09
当我们做项目时经常会用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,,让人有一种去死的感觉,可读性也很差。
所以我们可以把相同布局的代码单独拿出来放在一个xml文件中,通过<include /> 标签来重用它。这样我们的代码显得比较清洁,一目了然。
读者对代码的整体布局有一个深入的了解。
1 include标签只有layout属性是必须的
2.include标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖
3 在include标签中所有的android:layout_*都是有效的。
但前提是必须要写layout_width和layout_height两个属性,否则无效 。
看一个例子:
main.xml
1
2
3
4
5
6
7
8
9
10
11
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<include
android:id=
"@+id/include"
layout=
"@layout/other"
/>
</LinearLayout>
include要引用的那个xml:other.xml
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<ImageView
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:src=
"@drawable/free_bg_small_3"
/>
</LinearLayout>
IncludeActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package
xiaosi.include;
import
android.app.Activity;
import
android.os.Bundle;
public
class
IncludeActivity
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
- Android实战经验之Incude便签
- Android实战经验之Incude便签
- Android实战经验之Incude便签
- Incude便签
- Android之高仿记事本、备忘录、便签
- Android之高仿记事本、备忘录、便签
- Android之仿小米便签程序
- Android开发实战经验
- 修复SQLSERVER2000之实战经验
- 【uWSGI】 实战之操作经验
- 【快捷键】window7之便签
- Android 开发资料便签
- Android 开发资料便签
- 便签
- 便签
- 便签
- Android开发--便签(一)
- Android实战经验之图像处理及特效处理的集锦(总结版)
- matlab笔记
- 第七章 视频层
- android 2.3 频率管理
- 如何使用java synchronized进行线程同步
- Linux操作系统下Sudo命令的使用方法说明
- Android实战经验之Incude便签
- 务虚
- android 详细信息显示界面的思路
- 如何写Vim脚本(一)
- 要开始学c语言了
- Linux Sudo的使用
- VB.Net Winform程序的简单打包和部署--<机房收费系统>
- swjtu oj 1579 加号问题
- android开发常用命令