Android_UI
* 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的更多相关文章
- Appium Android Bootstrap源码分析之简介
在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium ...
- Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制
Android开发 ---基本UI组件4 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding=" ...
- Android开发 ---基本UI组件3:单选按钮、多选按钮、下拉列表、提交按钮、重置按钮、取消按钮
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个用户注册按钮 <?xml version="1.0" encoding=&q ...
- Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...
- Android开发---基本UI组件1:自动拨电话,自动上网,输入框不换行、只输数字、只输文本、只输密码
1.activity_main.xml 描述:构建一个按钮 <?xml version="1.0" encoding="utf-8"?> <L ...
- Appium Android Bootstrap源代码分析之简单介绍
在上一个系列中我们分析了UiAutomator的核心源代码,对UiAutomator是怎么执行的原理有了根本的了解.今天我们会開始另外一个在安卓平台上基于UiAutomator的新起之秀--Appiu ...
- (转载)Android之有效防止按钮多次重复点击的方法(必看篇)
为了防止测试妹子或者用户频繁点击某个按钮,导致程序在短时间内进行多次数据提交or数据处理,那到时候就比较坑了~ 那么如何有效避免这种情况的发生呢? 我的想法是,判断用户点击按钮间隔时间,如果间隔时间太 ...
随机推荐
- 错误 1 在应用程序级别之外使用注册为 allowDefinition='
原文:错误 1 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的,银流沙 昨天运行一个.NET网站项目时,出现了以下问题: 在应 ...
- application/x-www-form-urlencoded等字符编码的解释说明
关于application/x-www-form-urlencoded等字符编码的解释说明 在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏 ...
- 【转】为什么C++编译器不能支持对模板的分离式编译
出处:刘未鹏(pongba) http://blog.csdn.net/pongba) 首先,一个编译单元(translation unit)是指一个.cpp文件以及它所#include的所有.h ...
- 常规页生命周期(class0620)
常规页声明周期阶段 阶段 说明 页请求 开始 页初始化 加载 验证 回发事件处理 卸载 生命周期事件 页事件 典型使用
- MyEclipse10 Tomcat7 JDK1.7 配置
第一步.MyEclipse10 Tomcat7 JDK1.7下载 MyEclipse10http://downloads.myeclipseide.com/downloads/products/ewo ...
- office在线预览方案
一.服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览 微软方:利用Office2007以上版本的一个PDF插件SaveAsPDFandXPS.exe可以导出PDF文件,然后再利用免 ...
- 秒杀 ILSpy 等反编译利器 DotNet Resolver
http://dotnetresolver.eu5.org/downloads.html DotNet Resolver is a free .NET decompiler written in C# ...
- android微信分享遇到的问题
1.WXWebpageObject.description 长度不能超过1024 2.若回调返回BaseResp.ErrCode.ERR_AUTH_DENIED(用户拒绝),请检查AppID是否填写正 ...
- Cocos2d-x 3.x学习笔记(一):开始Cocos2d之旅
首先,进入官网下载cocos2d-x:http://www.cocos.com/download/,当然你需要注册一个账号才可以下载. 接下来需要跟着官网的配置文档配置一下开发环境,不得不说,Coco ...
- 【Stage3D学习笔记续】真正的3D世界(六):空间大战
这就是书上的最终效果了,一个完整的空间大战游戏: 点击查看源码 这里并没有太多的新知识,所涉及的东西更多的是游戏开发方面的优化和技巧,下面我们大家一起来看看: 飞船: 类似粒子效果中的粒子创建方法,我 ...