【include+LinearLayout】的使用例子

AndroidIncludeLayout.java

  1. package com.AndroidIncludeLayout;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9.  
  10. public class AndroidIncludeLayout extends Activity {
  11.  
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16.  
  17. View subLayout1 = (View)findViewById(R.id.main1);
  18. View subLayout2 = (View)findViewById(R.id.main2);
  19. Button myButton_main1 = (Button)subLayout1.findViewById(R.id.mybutton);
  20. Button myButton_main2 = (Button)subLayout2.findViewById(R.id.mybutton);
  21. Button startAnotherActivity = (Button)findViewById(R.id.startanotheractivity);
  22.  
  23. startAnotherActivity.setOnClickListener(new Button.OnClickListener(){
  24.  
  25. @Override
  26. public void onClick(View arg0) {
  27. // TODO Auto-generated method stub
  28. Intent intent = new Intent();
  29. intent.setClass(AndroidIncludeLayout.this, AnotherActivity.class);
  30. startActivity(intent);
  31.  
  32. }});
  33.  
  34. myButton_main1.setOnClickListener(new Button.OnClickListener(){
  35.  
  36. @Override
  37. public void onClick(View arg0) {
  38. // TODO Auto-generated method stub
  39. Toast.makeText(AndroidIncludeLayout.this, "Button 1 Pressed", Toast.LENGTH_LONG).show();
  40. }});
  41.  
  42. myButton_main2.setOnClickListener(new Button.OnClickListener(){
  43.  
  44. @Override
  45. public void onClick(View arg0) {
  46. // TODO Auto-generated method stub
  47. Toast.makeText(AndroidIncludeLayout.this, "Button 2 Pressed", Toast.LENGTH_LONG).show();
  48. }});
  49. }
  50. }

mail.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <include android:id="@+id/main1" layout="@layout/sublayout" />
  13. <include android:id="@+id/main2" layout="@layout/sublayout" />
  14. <Button
  15. android:id="@+id/startanotheractivity"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text=" Start Another Activity "
  19. />
  20. </LinearLayout>

sublayout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:background="#505050"
  7. >
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="SubLayout"
  12. />
  13. <Button
  14. android:id="@+id/mybutton"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text=" A Button "
  18. />
  19. </LinearLayout>

【include+merge】   //与上面的include用法有点不一样

LightActivity.java

  1. /**
  2. * Copyright(c) 2014-2015 ChinaYong Hotel Media Technology Co.,Ltd.
  3. * All Rights Reserved.
  4. *
  5. * Filename : LightActivity.java
  6. * Author : Seldy lipeineng
  7. * Creation time : 上午10:58:53 - 2015-6-4
  8. * Description :
  9. */
  10. package com.hysmarthotel.roomcontrol;
  11.  
  12. import com.hysmarthotel.util.LogUtil;
  13. import com.hysmarthotel.view.Temperature;
  14.  
  15. import android.app.Activity;
  16. import android.os.Bundle;
  17. import android.view.View;
  18. import android.widget.TextView;
  19. //灯光控制
  20. public class LightActivity extends Activity {
  21. public static Temperature temp;
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.light);
  26. temp = (Temperature)findViewById(R.id.temperature_light); //特殊的地方是,可以直接一步调用merge中的id,不用通过include,再去调用
  27. log("layout1"+temp);
  28. init();
  29. }
  30. private void init() {
  31. temp.setTemperature(MainActivity.mTemp+"");
  32. }
  33. private void log(String msg) {
  34. LogUtil.info(this.getClass(), this + ":" + msg,"i");
  35. }
  36. }

light.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/bg1" >
  6. <TextView
  7. android:id="@+id/lightctrl"
  8. android:layout_x="91.5px"
  9. android:layout_y="93.0px"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:gravity="center_vertical"
  13. android:textSize="42.0px"
  14. android:textColor="#fff3e3d1"
  15. android:text="@string/light_ctrl"
  16. android:drawableLeft="@drawable/ic_light_t"
  17. android:drawablePadding="6px"/>
  18.  
  19. <include android:id="@+id/include1" layout="@layout/time_temp"/>
  20. </AbsoluteLayout>

time_temp.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <merge xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:hyhotel="http://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol" >
  4.  
  5. <com.hysmarthotel.view.Temperature
  6. android:id="@+id/temperature_light"
  7. android:layout_x="1545.0px"
  8. android:layout_y="109.5px"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:textSize="22.5px"
  12. android:textColor="#fff3e3d1"
  13. hyhotel:prefix="@string/room_temp"
  14. hyhotel:unit="@string/celsius" />
  15. </merge>

Android 布局优化 include+merge+ViewStub标签详解

merge标签

merge用于消除视图层次结构中的冗余视图,例如根布局是Linearlayout,那么我们又include一个LinerLayout布局就没意义了,反而会减慢UI加载速度。

又或者根布局是FrameLayout且不需要设置background或padding等属性,可以用merge代替,因为Activity的ContentView父元素就是FrameLayout,所以可以用merge消除只剩一个

ViewStub标签

ViewStub 标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局比如进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。

由于ViewStub第一次inflate的时候,就已经将需要显示的布局替换掉自己了,所以第二次inflate的时候,getParent()是null,所以就会报异常。解决方法是inflate()的时候将view保存起来,然后下次判断这个View是否为NUll,如果是null就inflate().否则就直接使用这个view。

include的用法例子,以及include+merge的用法例子的更多相关文章

  1. JSP指令include和JSP动作元素include的区别

    include指令用于在JSP页面静态的包含一个文件,该文件可以是JSP页面.HTML页面.文本文件或者一段java代码.使用include指令的JSP页面在转换时,JSP容器会在其中插入所包含文件的 ...

  2. pandas-16 pd.merge()的用法

    pandas-16 pd.merge()的用法 使用过sql语言的话,一定对join,left join, right join等非常熟悉,在pandas中,merge的作用也非常类似. 如:pd.m ...

  3. SQL2008中Merge的用法

    在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...

  4. SQL中Merge的用法

    SQL中Merge的用法 Merge的用法 Merge可以完成以下功能: 1.  两个表之间数据的更新 2.  进行进销存更新库存 3.  进行表之间数据的复制 语法说明: 1.  在语句结束后一定要 ...

  5. SQL2008中Merge的用法(轉載)

    在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...

  6. SQL2008中Merge的用法(转)

    在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...

  7. day319 1、正则表达式的定义及使用 2、Date类的用法 3、Calendar类的用法

    1.正则表达式的定义及使用2.Date类的用法3.Calendar类的用法 一.正则表达式 ###01正则表达式的概念和作用* A: 正则表达式的概念和作用* a: 正则表达式的概述* 正则表达式也是 ...

  8. (二)Hadoop例子——运行example中的wordCount例子

    Hadoop例子——运行example中的wordCount例子 一.   需求说明 单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为 MapReduce版"Hello ...

  9. Oracle 的merge into 用法

    1.merge into的用途 Merge是一个非常有用的功能,与DB2中的merge into功能几乎一样,与Mysql里的insert into on duplicate key也很类似.MERG ...

随机推荐

  1. ASP.NET MVC5 网站开发实践(一) - 项目框架

    前几天算是开题了,关于怎么做自己想了很多,但毕竟没做过项目既不知道这些想法有无必要,也不知道能不能实现,不过邓爷爷说过"摸着石头过河"吧.这段时间看了一些博主的文章收获很大,特别是 ...

  2. 深入理解DOM事件机制系列第一篇——事件流

    × 目录 [1]历史 [2]事件冒泡 [3]事件捕获[4]事件流 前面的话 javascript操作CSS称为脚本化CSS,而javascript与HTML的交互是通过事件实现的.事件就是文档或浏览器 ...

  3. 如何使用Xmanager及VNC登录远程桌面

    如何调用远程桌面,比较常见的有两种方式:Xmanager及VNC 正好今天鼓捣了一下,特整理如下: Xmanager Xmanager的调用也有两种方式: 一.直接在Xshell中调用 这时需设置会话 ...

  4. 【记录】VS2012新建MVC3/MVC4项目时,报:此模板尝试加载组件程序集“NuGet.VisualStudio.Interop...”

    最近电脑装了 VisualStudio "14" CTP,由于把其他版本的 VS 卸掉,由高到低版本安装,当时安装完 VisualStudio "14" CTP ...

  5. android 伸缩控件ExpandableListView 展开失败的可能原因。

    (原创)转载请声明出处http://www.cnblogs.com/linguanh/ 问题原型: ExpandableListView 展开失效. --------------------直接看结论 ...

  6. Hadoop入门学习笔记---part2

    在<Hadoop入门学习笔记---part1>中感觉自己虽然总结的比较详细,但是始终感觉有点凌乱.不够系统化,不够简洁.经过自己的推敲和总结,现在在此处概括性的总结一下,认为在准备搭建ha ...

  7. TreeView控件使用

    treeView1.SelectedNode = treeView1.Nodes[0];  //选中当前treeview控件的根节点为当前节点添加子节点:  TreeNode tmp; tmp = n ...

  8. canvas刮刮乐和画笔

    这周有点迷茫,不知道干嘛了,一天天就过去了!我在博客右侧公告栏加了qq交流,各位有好的主题,或者有趣的技术,欢迎交流!今天突发奇想,就写了2个h5 canvas的demo玩玩! demo一:刮刮乐 舍 ...

  9. MongoDB - basic

    mongoDB basic from:http://www.tutorialspoint.com/mongodb prject:https://github.com/chenxing12/l4mong ...

  10. jquery技巧之让任何组件都支持类似DOM的事件管理

    本文介绍一个jquery的小技巧,能让任意组件对象都能支持类似DOM的事件管理,也就是说除了派发事件,添加或删除事件监听器,还能支持事件冒泡,阻止事件默认行为等等.在jquery的帮助下,使用这个方法 ...