在一个项目中,我们可能会在xml中局部用到相同的布局,如果每次都在xml中重写这些布局,代码显得很冗余、重复的复制黏贴也很烦恼,所以,我们把这些相同的局部布局写成一个单独的xml模块,需要用到这些布局时,在要使用的xml中引入这些布局,而引用布局时所需使用的头标签就是<include />。

现在我们来看一下代码:

item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <Button
  7. android:id="@+id/btn"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="按钮"/>
  11. <EditText
  12. android:id="@+id/edt"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="123456"/>
  16. </LinearLayout>

item.xml 图示:

activity_main.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/tv"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello_world"
  11. android:paddingBottom="20dp"/>
  12. <include
  13. android:id="@+id/my_item"
  14. layout="@layout/item"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"/>
  17. <include
  18. android:id="@+id/my_item2"
  19. layout="@layout/item"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"/>
  22. </LinearLayout>

activity_main.xml 图示:

MainActivity.java

  1. package com.example.includedemo;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11. public class MainActivity extends Activity {
  12. private LinearLayout lln1,lln2;
  13. private TextView tv;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. tv = (TextView) findViewById(R.id.tv);
  19. //初始化xml中id为my_item的控件中的button
  20. lln1 = (LinearLayout) findViewById(R.id.my_item);
  21. lln1.setBackgroundColor(Color.BLUE);
  22. Button btn1 = (Button)lln1. findViewById(R.id.btn);
  23. //初始化xml中id为my_item2的控件中的button
  24. lln2 = (LinearLayout) findViewById(R.id.my_item2);
  25. lln2.setBackgroundColor(Color.GREEN);
  26. Button btn2 = (Button)lln2. findViewById(R.id.btn);
  27. btn1.setOnClickListener(new OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. tv.setText("第一个 include中的button");
  31. }
  32. });
  33. btn2.setOnClickListener(new OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. tv.setText("第二个 include中的button");
  37. }
  38. });
  39. }
  40. }

android xml中使用include标签的更多相关文章

  1. Android在layout xml中使用include

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

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

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

  3. Android在layout xml中使用include完成静态加载

    Android在layout xml中使用include完成静态加载 include静态加载:不仅可以加载布局,还可以加载控件(控件标签名要在最外层)include标签中有个layout属性就是专门用 ...

  4. 【转】 Android xml中 @和?区别,style和attr小结

    引用资源时,使用@还是?的区别,例如在设置style的时候既可以使用@也可以使用? style="?android:attr/progressBarStyleHorizontal" ...

  5. 【转】在Android布局中使用include和merge标签

    内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...

  6. Android在layout xml中使用include[转]

    在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的.例如: <include layout="@layo ...

  7. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  8. Android里merge和include标签的使用

    1.使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在andr ...

  9. Web.xml中Filter过滤器标签几个说明

    在研究liferay框架中看到Web.xml中加入了过滤器的标签,可以根据页面提交的URL地址进行过滤,发现有几个新标签没用过,下面就介绍以下几个过滤器的标签用法: <!-- 定义Filter ...

随机推荐

  1. contents属性

    CALayer 有一个属性叫做contents,这个属性的类型被定义为id,意味着它可以是任何类型的对象.在这种情况下,你可以给contents属性赋任何值,你的app仍然能够编译通过.但是,在实践中 ...

  2. codeforces 667B B. Coat of Anticubism(水题)

    题目链接: B. Coat of Anticubism time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. 【CAIOJ 1178】 最长共同前缀长度

    [题目链接] 点击打开链接 [算法] EXKMP [代码] #include<bits/stdc++.h> using namespace std; #define MAXL 100001 ...

  4. MongoDb复制集实现故障转移,读写分离

    前言 数据库技术是信息系统的一个核心技术,发展很快,各种功能类型数据库层出不穷,之前工作中使用过关系型数据库(mysql.oracle等).面相对象数据库(db4o).key-value存储(Memc ...

  5. mtr和nmap命令

    mtr mtr是一个网络连通性判断工具,它可以结合ping nslookup tracert 来判断网络的相关特性. [root@10.10.90.97 ~]# mtr -h usage: mtr [ ...

  6. Python2/3共存,pip2/3共存

    python2 和 python3设置: 1.安装Python2和3,并添加环境变量: 2.找到python2和python3的安装目录,修改python2中和python3中python.exe和p ...

  7. 006--linux基础rpm和yum的使用和源码安装

    一.rpm相关命令介绍 1. 查看CD里面有的文件 2. 用rpm来安装一个名为vsftpd的rpm包 3. rpm -qi 软件包名 (查看软件包的详细信息) 4. rpm -ql 软件包名 (查看 ...

  8. hihoCoder 1032

    最长回文子串的O(1)算法Manacher算法 #include <iostream> #include <stdio.h> #include <string.h> ...

  9. ErrorObject OpenAsync(Action<ErrorObject>arg_fnRet)

    ErrorObject OpenAsync(Action<ErrorObject>arg_fnRet) public static ErrorObject Open(this ReadWr ...

  10. bzoj 2882: 工艺【SAM】

    看上去比较SA,但是在学SAM所以就用SAM来做-- 把串复制一遍接在后面,对这个新串求SAM(这里的儿子节点要用map转移),然后从根节点每次都向最小的转移走,这样走n次转移的串就是答案 #incl ...