AndrodUI优化之布局优化

前些天在 那些年大家都在谈论的Android性能优化 中已经跟大家分享了Android中性能优化的概念,优化的一些关键点以及优化方案。今天跟大家探讨一下AndroidUI优化中的布局优化具体是怎么操作的。

选择合适的ViewGroup

1.FrameLayout和LinearLayout是Android中比较简单并且高效的viewgroup,如果不考虑布局嵌套的话,尽量使用这两种。
2.大多数情况单纯使用FrameLayout或者LinearLayout无法实现产品效果,需要进行布局嵌套,这种情况尽量使用RelativeLayout。嵌套会增加布局的层级,降低App的性能。大约在Android4.0之后,新建工程的默认main.xml中顶节点改成了RelativeLayout,因为RelativeLayout性能更优,可以简单实现LinearLayout嵌套才能实现的布局。

抽象布局标签

include

include主要用于布局重用,将布局中的公共部分抽离出来供其他布局用,实现布局的模块化,比如App的顶栏等。

<?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">

    <include
        android:id="@+id/title"
        layout="@layout/layout_title" />

    <android.support.v4.view.ViewPager
        android:id="@+id/fragment_home_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

include标签唯一需要的属性是layout属性,指定需要包含的布局文件。可以定义android:id和android:layout_*属性来覆盖被引入布局根节点的对应属性值。注意重新定义android:id后,子布局的顶结点id就变化了。如果被指定了android:layout_*属性,那么android:layout_width和android:layout_height也必须存在,否则其他android:layout_*属性不生效。

merge

merge标签一般和include标签一起使用,可以减少布局的层级。假如我的layout_title文件中的根节点也是竖直方向的linearlayout,我们可以通过merge标签来减少这一层linearlayout。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</merge>

另一种情况是当布局顶结点是FrameLayout且不需要设置background或padding等属性,可以用merge代替,因为Activity内容视图的parent view就是个FrameLayout,所以可以用merge消除只剩一个。

viewstub

viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,不参与任何的布局和绘制过程,从而在解析layout时节省cpu和内存。它的意义在于按需加载布局文件,比如网络异常的界面正常情况下是不会显示的,我们就没有必要在初始化的时候就把它加载进来。

<?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">

    <include
        android:id="@+id/title"
        layout="@layout/layout_title" />

    <android.support.v4.view.ViewPager
        android:id="@+id/fragment_home_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ViewStub
        android:id="@+id/network_error_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/network_error" />
</LinearLayout>

network_error的内容

<?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="match_parent" >

    <Button
        android:id="@+id/network_setting"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/network_setting" />

    <Button
        android:id="@+id/network_refresh"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/network_setting"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/dp_10"
        android:text="@string/network_refresh" />

</RelativeLayout>

如果要显示viewstub中的内容,可以通过setVisibility或者inflate进行加载。

private View networkErrorView;

private void showNetError() {
    //保证不重复加载
    if (networkErrorView != null) {
        networkErrorView.setVisibility(View.VISIBLE);
        return;
    }

    ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);
    networkErrorView = stub.inflate();
    Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting);
    Button refresh = (Button)findViewById(R.id.network_refresh);
}

private void showNormal() {
    if (networkErrorView != null) {
        networkErrorView.setVisibility(View.GONE);
    }
}

减少不必要的infalte

1.对于inflate的布局可以直接缓存,用全部变量代替局部变量,避免下次需再次inflate
2.ListView提供了item缓存,adapter getView的标准写法,如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        ……
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }
}

/**
 * ViewHolder
 * 
 * @author trinea@trinea.cn 2013-08-01
 */
private static class ViewHolder {

    ImageView appIcon;
    TextView  appName;
    TextView  appInfo;
}

决定伟大水平和一般水平的关键因素,既不是天赋,也不是经验,而是刻意练习的程度。从下一页面开始,希望大家都能按照这些标准去写,刻意练习,熟能生巧。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值