* fang@author
* Android布局原则:
* (1)尽量多使用LinearLayout和RelativeLAyout,不要使用AbsoluteLayout
* (2)在布局层次一样的情况下,建议使用LinearLayout代替RelativeLayout,因为LinearLayout性能要稍高一些
* (3)将可复用的组件抽取出来并通过include标签使用
* (4)使用viewStub标签来加载一些不常用的布局
* (5)使用merge标签减少布局的嵌套层次
* UI布局优化:
* <include/>的使用
* 作用:将共有的组件抽取出来单独放到一个xml文件中,然后使用include便签导入共用布局
* 效果:提高UI的制作和复用效率,也能保证制作的UI布局更加规整和易维护
如果include指定了id的话,就不能直接把它里面的控件当成主xml中的控件来直接获得了,必须先获得这个xml布局文件,再通过布局文件findViewById来获得其子控件。
代码如下
*
*使用merge合并UI布局
* 作用:合并UI布局,使用该标签能降低UI布局的嵌套层次
*
*场景一:布局根结点是FrameLayout且不需要设置background或padding属性,可以用merge代替
*场景二:某布局作为子布局被其他布局include时,使用merge当做该布局 的顶节点,这样在被引入时顶节点会被自动忽略
*
*merge的使用:使用include引入一个布局文件,由于布局文件是放在一个ViewGroup中的。如果这个ViewGroup没有指定背景或者padding
*之类的其实就没必要存在,而且会多出一个层级。可以使用merge标签代替这个标签来进行优化。
*
*使用ViewStub惰性加载:默认不加载到内存中
*
*作用:ViewStub标签同include标签一样可以用来引入一个外部布局,
*不同的是,Viewstub引入的布局默认不会扩张,既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存
*
*/

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"
tools:context="com.example.android_ui.MainActivity" > <include layout="@layout/common_title" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<include layout="@layout/common_progressbar"/>"
<TextView
android:id="@+id/text04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="正文内容" />
</FrameLayout> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示隐藏内容"/>
<ViewStub
android:id="@+id/viewStub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout = "@layout/commontext"
/>
</LinearLayout>

commom_titile.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:text="返回"
/>
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ffffff"
android:layout_centerInParent="true"
android:text="布局优化"
/>
<TextView
android:id="@+id/text03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:text="功能"/> </RelativeLayout>

common_progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text_wait"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载中"
/>"
</merge>

commontext.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" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隐藏内容"
/>
</LinearLayout>

main.java

package com.example.android_ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
/**
public class MainActivity extends Activity { private Button btn;
private ViewStub stub; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
stub = (ViewStub) findViewById(R.id.viewStub);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
stub.inflate();//默认不加载该布局到内存,当点击按钮时,该布局才会加载到内存中
}
});
} }

Android_UI的更多相关文章

  1. Appium Android Bootstrap源码分析之简介

    在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium ...

  2. Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制

    Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...

  3. Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个用户注册按钮 <?xml version="1.0" encoding=&q ...

  4. Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...

  5. Android开发---基本UI组件1:自动拨电话,自动上网,输入框不换行、只输数字、只输文本、只输密码

    1.activity_main.xml 描述:构建一个按钮 <?xml version="1.0" encoding="utf-8"?> <L ...

  6. Appium Android Bootstrap源代码分析之简单介绍

    在上一个系列中我们分析了UiAutomator的核心源代码,对UiAutomator是怎么执行的原理有了根本的了解.今天我们会開始另外一个在安卓平台上基于UiAutomator的新起之秀--Appiu ...

  7. (转载)Android之有效防止按钮多次重复点击的方法(必看篇)

    为了防止测试妹子或者用户频繁点击某个按钮,导致程序在短时间内进行多次数据提交or数据处理,那到时候就比较坑了~ 那么如何有效避免这种情况的发生呢? 我的想法是,判断用户点击按钮间隔时间,如果间隔时间太 ...

随机推荐

  1. jsp、js、html等

    1.一个button标签怎么触发事件: 一般触发事件有两种方式,要么是在html直接绑定,即button标签中不只有class.type和id,还要写onclick=... 还有一种,就是在js代码部 ...

  2. POJ 3107-Godfather(树形dp)

    题意: 有n个节点的树,删除一个点,得到的最大联通分支最小,求这样点的集合 分析: dp[i]表示删除i所得最大联通分支,遍历一遍节点即可,该题用vector会超时 #include <map& ...

  3. [MySQL-1] mysql error 1101 blob/text column can't have a default value

    在MySQL Query Browser上创建一个含有TEXT类型的字段,创建不成功,报错:mysql error 1101 blob/text column can't have a default ...

  4. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (五) 如何让窗体记录登录状态Session

    在写自动化测试的Node.js脚本时, 时常需要测试所写的case, 可能都需要去重新登录一遍,这将相当的耗时, 好在Selenium都借了Session的机制, 如果在最初的浏览器没有关闭的情况下, ...

  5. C#打印100以内质数

    bool b = false; ; i < ; i++) { ; j < i; j++) { ) { b = false; break; } else { b = true; } } if ...

  6. Linux创建公钥

    A:192.168.1.1  B:192.168.1.2 现在想让A无密码登陆B机器 A上运行以下命令来生成公钥和私钥 ssh-keygen -t rsa -P '' 运行该命令后会生成如下两个文件 ...

  7. 【JS】Advanced1:Object-Oriented Code

    Object-Oriented Code 1. var Person = function (name) { this.name = name; }; Person.prototype.say = f ...

  8. 【整理】C++虚函数及其继承、虚继承类大小

    参考文章: http://blog.chinaunix.net/uid-25132162-id-1564955.html http://blog.csdn.net/haoel/article/deta ...

  9. Extjs4 MVC Controlller中refs使用

    前面几篇写了一下mvc的整体使用方法,今天写一下controller中refs的试用,refs的作用类似于我们告诉controller我们的一个元素的别名,既alias,那么controller就会为 ...

  10. 3.1 全局存储带宽与合并访问 -- Global Memory(DRAM) bandwidth and memory coalesce

    全局存储带宽(DRAM) 全局内存是动态随机访问的方式访问内存.我们希望访问DRAM的时候非常快,实际情况是DRAM中出来的数据非常非常慢,这就好比,理想状态是泄洪,水倾巢而出,气势宏伟,实际取水却像 ...