android xml中使用include标签
在一个项目中,我们可能会在xml中局部用到相同的布局,如果每次都在xml中重写这些布局,代码显得很冗余、重复的复制黏贴也很烦恼,所以,我们把这些相同的局部布局写成一个单独的xml模块,需要用到这些布局时,在要使用的xml中引入这些布局,而引用布局时所需使用的头标签就是<include />。
现在我们来看一下代码:
item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="按钮"/>
- <EditText
- android:id="@+id/edt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="123456"/>
- </LinearLayout>
item.xml 图示:
activity_main.xml:
- <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">
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world"
- android:paddingBottom="20dp"/>
- <include
- android:id="@+id/my_item"
- layout="@layout/item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <include
- android:id="@+id/my_item2"
- layout="@layout/item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
activity_main.xml 图示:
MainActivity.java
- package com.example.includedemo;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Color;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private LinearLayout lln1,lln2;
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv = (TextView) findViewById(R.id.tv);
- //初始化xml中id为my_item的控件中的button
- lln1 = (LinearLayout) findViewById(R.id.my_item);
- lln1.setBackgroundColor(Color.BLUE);
- Button btn1 = (Button)lln1. findViewById(R.id.btn);
- //初始化xml中id为my_item2的控件中的button
- lln2 = (LinearLayout) findViewById(R.id.my_item2);
- lln2.setBackgroundColor(Color.GREEN);
- Button btn2 = (Button)lln2. findViewById(R.id.btn);
- btn1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- tv.setText("第一个 include中的button");
- }
- });
- btn2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- tv.setText("第二个 include中的button");
- }
- });
- }
- }
android xml中使用include标签的更多相关文章
- Android在layout xml中使用include
Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...
- android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!
转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...
- Android在layout xml中使用include完成静态加载
Android在layout xml中使用include完成静态加载 include静态加载:不仅可以加载布局,还可以加载控件(控件标签名要在最外层)include标签中有个layout属性就是专门用 ...
- 【转】 Android xml中 @和?区别,style和attr小结
引用资源时,使用@还是?的区别,例如在设置style的时候既可以使用@也可以使用? style="?android:attr/progressBarStyleHorizontal" ...
- 【转】在Android布局中使用include和merge标签
内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...
- Android在layout xml中使用include[转]
在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的.例如: <include layout="@layo ...
- Android XML中引用自定义内部类view的四个why
今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...
- Android里merge和include标签的使用
1.使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在andr ...
- Web.xml中Filter过滤器标签几个说明
在研究liferay框架中看到Web.xml中加入了过滤器的标签,可以根据页面提交的URL地址进行过滤,发现有几个新标签没用过,下面就介绍以下几个过滤器的标签用法: <!-- 定义Filter ...
随机推荐
- BestCoder4 1002 Miaomiao's Geometry (hdu 4932) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1 ...
- hdu 2066 一个人的旅行 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目意思:给出T条路,和草儿家相邻的城市编号,以及草儿想去的地方的编号.问从草儿家到达草儿想去的 ...
- java回调机制及其实现
1. 什么是回调函数 回调函数,顾名思义,用于回调的函数.回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机.回调 ...
- java8的常用的新特征
一.Java 8 对接口做了进一步的增强.(默认方法和静态方法) a. 在接口中可以添加使用 default 关键字修饰的非抽象方法.即:默认方法(或扩展方法不用被实现)如:comparator接口. ...
- 【前端】CentOS 7 系列教程之三: 搭建 git 服务器
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_3.html 上一篇我们安装好了git,这一篇我们搭建git服务器 创建一个用户组 groupadd g ...
- 洛谷P2473奖励关——状压DP
题目:https://www.luogu.org/problemnew/show/P2473 还是对DP套路不熟悉... 像这种前面影响后面,而后面不影响前面的问题就应该考虑倒序递推: 看n只有15那 ...
- web前端安全机制问题全解析
摘要 web前端安全方面技术含有的东西较多,这里就来理一理web安全方面所涉及的一些问题 目录[-] 摘要 web前端安全方面技术含有的东西较多,这里就来理一理web安全方面所涉及的一些问题 web安 ...
- 简析hotjar录屏功能实现原理
简析hotjar录屏功能实现原理 众所周知,hotjar中录屏功能是其重要的一个卖点,看着很牛X酷炫的样子,今天就简单的分析一下其可能实现(这里只根据其请求加上个人理解分析,并不代表hotjar中真实 ...
- 洛谷 - P1034 - 矩形覆盖 - dfs
https://www.luogu.org/problemnew/show/P1034 可能是数据太水了瞎搞都可以过. 判断两个平行于坐标轴的矩形相交(含顶点与边相交)的代码一并附上. 记得这里的xy ...
- hdu1068 Girls and Boys 基础匈牙利
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...