* 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. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.2

    The elementary tensors $x\otimes \cdots \otimes x$, with all factors equal, are all in the subspace ...

  2. 点亮一个led

    1:RS232电平:计算机串口 高电平为-12v,低电平为+12v,所以计算机与单片机进行通信的时候需要加电平转换芯片max232(高电平为-9到-12,低电平为+3到+12之间的. max232通常 ...

  3. Swift之基础知识

    Swift之基础知识 出于对Swift3.0的学习,写下这篇基本语法的笔记.希望能帮助记忆 -0- 这边提供Swift3.0中文教材,资源链接: https://pan.baidu.com/s/1c2 ...

  4. 在windows xp 平台上安装mvc4失败

    使用web 平台安装程序,在windows xp上安装mvc4 出现失败,需要主要是windows powershell 2.0安装失败,需要先卸载power shell 1.0或者 winowrm ...

  5. Java集合的小抄 Java初学者必备

    在尽可能短的篇幅里,将所有集合与并发集合的特征,实现方式,性能捋一遍.适合所有”精通Java”其实还不那么自信的人阅读. 不断更新中,请尽量访问博客原文. List ArrayList 以数组实现.节 ...

  6. Hadoop MapReduce概念学习系列之JobTracker、ResourceManager、Task Tracker、NodeManager(二十一)

    Tracker是跟踪者,跟踪器.JobTracker是项目经理.在hadoop2*的0.23版本之后,改叫RM了.ResourceManager.TaskTracker是小组长.它手下,还有具体搬砖的 ...

  7. 起底多线程同步锁(iOS)

    iOS/MacOS为多线程.共享内存(变量)提供了多种的同步解决方案(即同步锁),对于这些方案的比较,大都讨论了锁的用法以及锁操作的开销,然后就开销表现排个序.春哥以为,最优方案的选用还是看应用场景, ...

  8. 转载Ajax.Net--ScriptManager和UpdatePanel控件

    ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因 ...

  9. FreeModbus Slave 改进的eMbPoll()【worldsing 笔记】

    eMbPoll()的作用是FreeMod协议通信过程中不断查询事件对列有无完速数据桢,并进行地址和CRD验证,最后运行和回复主机. 为了减小代码尺寸对eMbPoll进行改进: 原版: 1:  2: e ...

  10. Servlet+Tomcat 界面登录

    1.文件夹建立(必须按照这个规范,文件名和文件夹名必须一致) a.在%TOMCAT_HOME%\webapps下建立一个文件夹,取名MyWebsit b.在MyWebsit文件夹下新建WEB_INF文 ...