Android布局的优化
来源:互联网 发布:miss淘宝外设店网址 编辑:程序博客网 时间:2024/06/03 12:30
mAndroid常用布局样式
使用量从高到低:
- RelativeLayout(相对布局)
- LinearLayout(线性布局)
- FrameLayout(帧布局)
- TableLayout(表格布局)
- AbsoluteLayout(绝对布局)
布局原则:
尽量多使用LinearLayout(线性布局)和RelativeLayout(相对布局),不要使用AbsoluteLayout(绝对布局)
在布局层次一样的情况下,建议使用LinearLayout代替RelativeLayout,因为LinearLayout性能要稍高一点
将可复用的组件抽取出来并通过include标签使用
使用ViewStub标签来加载一些不常用的布局
使用merge标签减少布局的嵌套层次
include标签的使用
作用:将公用的组件抽取出来单独放到一个xml文件中,然后使用include标签导入公用布局
效果:提高UI的制作的复用效率,也能保证制作的UI布局更加规范和已维护
<include layout="@layout/common_progressbar"//这是引用的layout文件/>
merge标签的使用
作用:合并UI布局,使用该标签能降低UI布局的嵌套层次
case 1:布局根节点是FrameLayout且不需要设置background或padding等属性,可以用merge代替
case 2:某布局作为子布局被其他布局include时,使用merge当该布局的顶节点,这样在被引入顶节点会自动被忽略
mergeTest01.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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:text="请稍后" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /></merge>
-
main.xml
<include layout="@layout/mergeTest01" />
ViewStub标签的使用
**作用:**Viewstub标签同include标签一样可以用来引入外部布局,不同的是,Viewstub引入的布局默认不会扩张,既不会占用显示也不会占用位置,从而在解析layout时节省CPU和内存
main.xml
<ViewStub android:id="@+id/stub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout="@layout/test02" />
-
test02.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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏内容" /></LinearLayout>
-
main.java 操作Viewstub
package com.example.android_layout;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewStub;import android.widget.Button;public class MainActivity extends Activity { private Button button; private ViewStub stub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //include标签所加载的layout空间,也可以在这里findbyid button =(Button) findViewById(R.id.button111); stub = (ViewStub) findViewById(R.id.stub); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //显示Viewstub里的布局View stub.inflate(); } }); }}
0 0
- Android布局的优化
- android的布局优化
- android布局的优化
- Android布局的优化
- 谈谈android 布局 的优化
- 谈谈android 布局 的优化
- 谈谈android 布局 的优化
- 谈谈android 布局 的优化
- Android界面布局的优化
- 谈谈android 布局 的优化
- 谈谈android 布局 的优化
- Android页面的布局优化
- 谈谈android 布局 的优化
- 谈谈android 布局 的优化
- 谈谈android 布局 的优化
- android 布局的性能优化
- Android layout布局的优化
- android布局优化的重要性
- Android Binder(zt)
- leetcode--Flatten Binary Tree to Linked List
- Sql Server如何新建作业
- 我的第一篇博客
- 使用MATLAB和UML进行嵌入式开发---四轴飞行器室内定位及导航系统 开篇(一)
- Android布局的优化
- android Binder
- myeclipse 上安装 Maven3
- android widget 开发实例 : 桌面便签程序的实现详解和源码 (上)
- 数据结构与算法——冒泡排序
- android widget 开发实例 : 桌面便签程序的实现详解和源码 (下)
- 文件I/O之fcntl函数
- Java初识面向对象
- Android 本地代码如何输出日志