【include+LinearLayout】的使用例子

AndroidIncludeLayout.java

package com.AndroidIncludeLayout; 

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class AndroidIncludeLayout extends Activity { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); View subLayout1 = (View)findViewById(R.id.main1);
View subLayout2 = (View)findViewById(R.id.main2);
Button myButton_main1 = (Button)subLayout1.findViewById(R.id.mybutton);
Button myButton_main2 = (Button)subLayout2.findViewById(R.id.mybutton);
Button startAnotherActivity = (Button)findViewById(R.id.startanotheractivity); startAnotherActivity.setOnClickListener(new Button.OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(AndroidIncludeLayout.this, AnotherActivity.class);
startActivity(intent); }}); myButton_main1.setOnClickListener(new Button.OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(AndroidIncludeLayout.this, "Button 1 Pressed", Toast.LENGTH_LONG).show();
}}); myButton_main2.setOnClickListener(new Button.OnClickListener(){ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(AndroidIncludeLayout.this, "Button 2 Pressed", Toast.LENGTH_LONG).show();
}});
}
}

mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<include android:id="@+id/main1" layout="@layout/sublayout" />
<include android:id="@+id/main2" layout="@layout/sublayout" />
<Button
android:id="@+id/startanotheractivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Start Another Activity "
/>
</LinearLayout>

sublayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#505050"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SubLayout"
/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" A Button "
/>
</LinearLayout>

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

LightActivity.java

/**
* Copyright(c) 2014-2015 ChinaYong Hotel Media Technology Co.,Ltd.
* All Rights Reserved.
*
* Filename : LightActivity.java
* Author : Seldy lipeineng
* Creation time : 上午10:58:53 - 2015-6-4
* Description :
*/
package com.hysmarthotel.roomcontrol; import com.hysmarthotel.util.LogUtil;
import com.hysmarthotel.view.Temperature; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
//灯光控制
public class LightActivity extends Activity {
public static Temperature temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.light);
temp = (Temperature)findViewById(R.id.temperature_light); //特殊的地方是,可以直接一步调用merge中的id,不用通过include,再去调用
log("layout1"+temp);
init();
}
private void init() {
temp.setTemperature(MainActivity.mTemp+"");
}
private void log(String msg) {
LogUtil.info(this.getClass(), this + ":" + msg,"i");
}
}

light.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1" >
<TextView
android:id="@+id/lightctrl"
android:layout_x="91.5px"
android:layout_y="93.0px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="42.0px"
android:textColor="#fff3e3d1"
android:text="@string/light_ctrl"
android:drawableLeft="@drawable/ic_light_t"
android:drawablePadding="6px"/> <include android:id="@+id/include1" layout="@layout/time_temp"/>
</AbsoluteLayout>

time_temp.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hyhotel="http://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol" > <com.hysmarthotel.view.Temperature
android:id="@+id/temperature_light"
android:layout_x="1545.0px"
android:layout_y="109.5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22.5px"
android:textColor="#fff3e3d1"
hyhotel:prefix="@string/room_temp"
hyhotel:unit="@string/celsius" />
</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. C#中的readonly与const的比较

    C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景.工作原理 readonly为运行时常量,程序运行时进行赋 ...

  2. 深入理解脚本化CSS系列第三篇——脚本化CSS类

    前面的话 在实际工作中,我们使用javascript操作CSS样式时,如果要改变大量样式,会使用脚本化CSS类的技术,本文将详细介绍脚本化CSS类 style 我们在改变元素的少部分样式时,一般会直接 ...

  3. VS2015 Enterprise 安装之惊险及收获

    前言 园子早早的就有人安装了VS 2015,自己也按捺不住了,也要赶快尝尝鲜!结果在其安装过程中一个小小的问题却困扰了我一天,这其中多亏了dudu耐心的解答才得以顺利完成,如果你也遇见这个问题,看过这 ...

  4. 编写简单的ramdisk(选择IO调度器)

    前言 目前linux中包含anticipatory.cfq.deadline和noop这4个I/O调度器.2.6.18之前的linux默认使用anticipatory,而之后的默认使用cfq.我们在前 ...

  5. MySQL参数

    1. sql_safe_updates 官方解释如下: , MySQL aborts . 默认为0,如果设置为1,则delete操作和update操作必须带有where子句,且where子句中的列必须 ...

  6. windows配置xhprof,PHP性能分析工具

    本来以为配置这么一个工具不会费很大的力气,后面发现完全不是. 一.小插曲 早上显示电脑不能显示虚拟目录下的所有域名,但是能打开localhost,数据库连接也不行了.这个问题纠缠了我一个上午.对了还有 ...

  7. 1Z0-053 争议题目解析698

    1Z0-053 争议题目解析698 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 698.In your production database, you: -Are using ...

  8. java多线程--几个多线程面试题小结

    自学了一段时间的多线程知识,尝试了做了几个编程题,发现想象中很简单的功能,自己真写起来要花费远超自己想象的功夫,知识点易学,不易用啊. 面试题1:编写程序实现,子线程循环10次,接着主线程循环20次, ...

  9. T-SQL CROSS APPLY、MERGE

    写在前面 刚才看项目里一个存储过程,也是好长时间没有使用Sql Server2008了,好多写法和函数感觉到陌生,这就遇到了CROSS APPLY 和MERGE的语法,两者之前完全没接触过. 所以专门 ...

  10. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...