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 ...
随机推荐
- read appSettings in configuration file by XElement with xmlns
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-write-queries- ...
- POJ-3352 Redundant Paths
In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) t ...
- hadoop部署之防火墙
在部署hadoop时,好多资料上都写了要关闭防火墙,如果不关闭可能出现节点间无法通信的情况,于是大家也都这样做了,因此集群通信正常.当然集群一般是处于局域网中的,因此关闭防火墙一般也不会存在安全隐患, ...
- POJ2443 Set Operation (基础bitset应用,求交集)
You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set" isn't ...
- 「NOIP2005」「Codevs1106」篝火晚会
题目描述 Description 佳佳刚进高中,在军训的时候,由于佳佳吃苦耐劳,很快得到了教官的赏识,成为了“小教官”.在军训结束的那天晚上,佳佳被命令组织同学们进行篝火晚会.一共有n个同学,编号从1 ...
- 【转】Intellij idea 的maven项目如何通过maven自动下载jar包
原文地址: https://blog.csdn.net/machao0903/article/details/73368909 maven项目自动加载jar包 所需工具如下: Intellij IDE ...
- 架构:MVC
ylbtech-架构:MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数 ...
- 025--python初识类和对象
一.面向对象的定义 说到面向对象,我们先来看一下面向过程的定义:面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么 ...
- Django中的Ajax详解
AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传输的数据为XML(当 ...
- kali的更新源
先安装的系统最好更新更新 apt-get update apt-get upgrade 因为默认会去国外的网站去下载....慢 为了速度 可以修改更新源 vim /etc/apt/sources.li ...