Android布局优化

来源:互联网 发布:成都管家婆软件代理 编辑:程序博客网 时间:2024/05/21 14:48

(一)检查和优化 Layout 层次

程序的每个组件和 Layout 都需要经过初始化、布局和绘制,如果布局嵌套层次过深,就会导致加载操作更为耗时,更严重的话还可能导致内存溢出。使用自带的HierarchyViewer工 能够从可视化的角度直观地获得布局设计结构,帮助优化布局设计。

(二)使用

(三)使用

<merge xmlns:android="http://schemas.android.com/apk/res/android"><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/><Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/></merge>

通过该标签,可减少层次避免嵌套过深的情况发生。

(四)ViewStub

ViewStub 是一个轻量视图,不需要大小信息,也不会在被加入的 Layout 中绘制任何东西,当你引入只在特殊情况才显示的布局,如进度条,出错信息,提示信息等布局时,就可以使用 ViewStub。

<ViewStub    android:id="@+id/stub_import"    android:inflatedId="@+id/panel_import"    android:layout="@layout/progress_overlay"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_gravity="bottom" />

其中 Android:id 指 ViewStub 的 id,仅在 ViewStub 可见之前使用。

inflatedId 是引入布局的 id。

加载 ViewStub

载入用 ViewStub 声明的布局有两种方式:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);// orView importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

(五)使用工具对代码进行 Lint 检查

Android Studio 可在菜单项 Analyze - Inspect Code,选择范围后对代码进行 Lint 检查,从检查结果中可以得到代码中不规范的编码,以便开发者进行修正。

本文简书地址:
http://www.jianshu.com/p/7a067d384cdd

0 0
原创粉丝点击