如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <include
android:id="@+id/cell1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
layout="@layout/item" /> <include
android:id="@+id/cell2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/cell1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/cell1"
layout="@layout/item" /> <include
android:id="@+id/cell3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/cell1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/cell2"
layout="@layout/item" /> </RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" > <ImageView
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="true"
android:focusable="false" /> <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_img"
android:layout_below="@+id/iv_img"
android:textColor="#a17006"
android:textSize="22dp"
android:textStyle="bold" /> </RelativeLayout>

使用Android include时需要注意的是要指定宽高属性,要不可能会出现一些意想不到的效果,比如引用了三次,而界面上只显示了一个item。

Android include的使用的更多相关文章

  1. android include进来的组件 调用其子元素

    include标签包裹着一个可复用的布局: <include layout="@layout/footer_detail" android:id="@+id/foo ...

  2. Android Include标签

    编程的世界有的时候很微妙,有的时候就好像是在解决一个哲学问题,Android开发的时候,所有的布局,颜色,等(其实这些都可以称之为资源,Android中的资源是指非代码部分,如图片.音频.视频.字符等 ...

  3. android include中的控件调用

    项目中经常会有一些布局是重用的,但是如何来更好的利用这些布局中的控件 转: http://zhidao.baidu.com/link?url=GU93U8Wu31dfp7mKEx52hMJkxjFLC ...

  4. Android include和merge标签的使用

    在Android布局文件中,某些时候使用include标签会很多的好处1,对于稍微有点复杂的布局界面,将所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差,这时可以分开使用include ...

  5. Android - include属性用法

    include属性用法 本文地址: http://blog.csdn.net/caroline_wendy Android的layout中, 能够使用include属性样式, 这样能够把不同的layo ...

  6. android include使用[转]

    需要包含的xml文件,我这里就放了一个Button按钮: btn.xml: <?xml version="1.0" encoding="utf-8"?&g ...

  7. android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!

    转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...

  8. Android在layout xml中使用include

    Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...

  9. 【Android 界面效果25】android中include标签的使用

    在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include ...

随机推荐

  1. 实现ios屏幕的横竖屏自适应

    整理总结中... 刷新 可以通过 -setNeedsUpdateConstraints -layoutIfNeeded 两个方法来刷新约束的改变,使UIView重新布局, 和CoreGraphic的- ...

  2. javascript学习笔记20160121-css选择器

    元素可以用id.标签名或类来描述: 更一般的,元素可以基于属性来选取: 这些基本的选择器可以组合使用: 选择器可以指定文档结构(重要,之前一直不太明白>的使用): 选择器可以组合起来选取多个或多 ...

  3. 写入.csv文件

    #include "stdafx.h" #include "WriteCsv.h" CString m_strData;//写入记录的一条数据 CString ...

  4. C++ DLL 获取 MSI Property

    VS2010 创建  C++, Win32 DLL工程C-TEST. Stdafx.h中,在<windows.h>之后 添加引用. #include <msi.h> #incl ...

  5. HIT 1867 经理的烦恼

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=1867 每次更新时判断是否素数,如果从非素数变成素数就Update(x, 1),如果从素数变成非素数就U ...

  6. 模板:qsort+bsearch应用

    (1)qsort: 功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)( ...

  7. 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考

    http://www.cnblogs.com/insus/p/4142264.html 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考 首先普世的编程架 ...

  8. Yii 关于 find findAll 查找出制定的字段的方法

    总所周知 modelName::model() -> find() //找出的是一个对象 modelName::model() -> findALL() //找出的是一个对象集合的数组 如 ...

  9. linux系统制作简单流程

    制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...

  10. web2py--------------用web2py写 django的例子 --------建立一个投票应用(2)

    建立模型 我们在models下及那里polls.py 文件内容如下 # -*- coding: utf-8 -*- pollsdb=DAL(uri='sqlite://polls.db') polls ...