android include使用[转]
需要包含的xml文件,我这里就放了一个Button按钮:
btn.xml:
- <?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="wrap_content"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button">
- </Button>
- </LinearLayout>
main.xml
- <?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/in1" layout="@layout/btn"/>
- <include android:id="@+id/in2" layout="@layout/btn"/>
- <TextView android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- </LinearLayout>
TestActivity:
- package com.hilary;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.hialry.R;
- /**
- *
- *@author:hilary
- *@Date:2011-12-8
- *@description:
- *
- **/
- public class TestActivity extends Activity {
- private TextView tv = null;
- private LinearLayout ll = null;
- private LinearLayout ll2 = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv = (TextView) findViewById(R.id.tv);
- //如果一个布局文件中包含同一个xml文件,这两个xml中的控件Id是一样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,
- //如果就包含同一个xml文件侧不需要此步操作
- ll = (LinearLayout) findViewById(R.id.in1);
- ll2 = (LinearLayout) findViewById(R.id.in2);
- ll.setBackgroundColor(Color.RED);
- Button btn = (Button) ll.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- tv.setText("My name is hilary");
- }
- });
- Button btn2 = (Button) ll2.findViewById(R.id.btn);
- btn2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- tv.setText(" You select second Button!");
- }
- });
- }
- }
这只是在xml文件中引入另一种布局的一种方法,我们还可以在代码中直接引入,而不需要在xml中定义要引入的文件,在这里就不多说了
from:http://hilary3113.iteye.com/blog/1297416
android include使用[转]的更多相关文章
- Android include的使用
如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下: main.xml <?xml version=&q ...
- android include进来的组件 调用其子元素
include标签包裹着一个可复用的布局: <include layout="@layout/footer_detail" android:id="@+id/foo ...
- Android Include标签
编程的世界有的时候很微妙,有的时候就好像是在解决一个哲学问题,Android开发的时候,所有的布局,颜色,等(其实这些都可以称之为资源,Android中的资源是指非代码部分,如图片.音频.视频.字符等 ...
- android include中的控件调用
项目中经常会有一些布局是重用的,但是如何来更好的利用这些布局中的控件 转: http://zhidao.baidu.com/link?url=GU93U8Wu31dfp7mKEx52hMJkxjFLC ...
- Android include和merge标签的使用
在Android布局文件中,某些时候使用include标签会很多的好处1,对于稍微有点复杂的布局界面,将所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差,这时可以分开使用include ...
- Android - include属性用法
include属性用法 本文地址: http://blog.csdn.net/caroline_wendy Android的layout中, 能够使用include属性样式, 这样能够把不同的layo ...
- android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!
转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...
- Android在layout xml中使用include
Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...
- 【Android 界面效果25】android中include标签的使用
在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include ...
随机推荐
- JFinal Web开发学习(二)目录、架构、package设计
package分类 config是JFinal的项目配置 controller是控制器 handler可以设置全局处理器,例如判断用户请求中是否直接请求 FreeMarker的模板文件ftl或者htm ...
- eclipse导入项目以后,内容没有错误,项目上却有个小红叉?
对于上面的错误,应该如何解决?
- Jenkins+Jmeter+Ant自动化集成及邮件正文以html输出
一.工具的安装与环境变量配置 1.依次安装Jenkins+Jmeter+Ant,具体安装步骤,此文不再详述 2.配置Jmeter&ant环境变量 Jmeter变量: 验证是否配置成功:cmd窗 ...
- JS 实现 jQuery的$(function(){});
1.浏览器渲染引擎的HTML解析流程 何谓“渲染”,其实就是浏览器把请求到的HTML内容显示出来的过程.渲染引擎首先通过网络获得所请求文档的内容,通常以8K分块的方式完成.下面是渲染引擎在取得内容之后 ...
- You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed问题解决
https://zeroturnaround.com/forums/topic/jrebel-reports-more-than-one-version-of-org-apache-commons-l ...
- Luogu 1169 [ZJOI2007]棋盘制作 - 动态规划+单调栈
Description 给一个01矩阵, 求出最大的01交错的正方形和最大的01交错的矩阵 Solution 用动态规划求出最大的正方形, 用单调栈求出最大的矩阵. 在这里仅介绍求出最大正方形(求最大 ...
- jsonp,ajax,json问题
JSONP技术 JSONP是解决跨域问题的一种常见方式 跨域问题,因为浏览器有同源策略,所以当不同域间进行数据交互的时候就会出现跨域问题 同源策略:只有在同协议.同域名.同端口的情况下才能进去数据交互 ...
- 通过修改Delphi 的 RTL,加快Delphi开发的应用程序速度和稳定性
RT 具体见PDF 看了不后悔,只给高手准备的. 神呐,偶看看是虾米东东
- Spring MVC 请求处理方法
以下两种都可以处理用户请求,但请求处理方法值得是第二种 1. SpringMVC 提供的 Controller 接口中公开的 ModelAndView handleRequest(request, r ...
- Netty 源码 NioEventLoop(三)执行流程
Netty 源码 NioEventLoop(三)执行流程 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 上文提到在启动 N ...