android中的布局优化

android中的布局优化

简介

用最少的view写出一样的效果,优化分为重用,合并,按需载入

#android中的dp、px、dip相关概念
px:是屏幕的像素点
dp:一个基于density的抽象单位,
px = dip * density / 160,则当屏幕密度为160时,px = dip
如果一个dpi的屏幕,1dp=1px
dip:等同于dp
TextView 的字号最好使用 sp 做单位,而且查看TextView的源码可知Android默认使用sp作为字号单位

布局相关的include、merge、viewstub标签控件作用及实现原理

可以允许在一个布局中引入另外一个布局。如果某几个界面有相同布局部分,可将相同的部分提前到一个独立的布局文件当中。然后每个布局界面来引用这个公共的布局。

在 include标签当中,我们是可以覆写所有layout属性的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">

    <include
        layout="@layout/stars_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/size_60dp" />
        .......
</RelativeLayout>

R.layout.stars_layout 界面编码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/star_num"
        android:textColor="@color/white"
        android:textSize="40sp"
        android:textStyle="bold"
       />

    <TextView
        android:id="@+id/tv_startNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="0"
        android:textColor="@color/color_common_red"
        android:textSize="60sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/num"
        android:textColor="@color/white"
        android:textSize="40sp"
        android:textStyle="bold"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/star_num" />
</LinearLayout>

merge

merge标签它的主要作用是为了防止在引用布局文件时产生多余的布局嵌套。Android去解析和展示一个布局是需要消耗时间的,布局嵌套的越多,那么解析起来就越耗时,性能也就越差,因此我们在编写布局文件时应该让嵌套的层数越少越好。
include它也存在着一个不好的地方,就是可能会导致产生多余的布局嵌套。
merge标签,这就表示当有任何一个地方去include这个布局时,会将merge标签内包含的内容直接填充到include的位置,不会再添加任何额外的布局结构。
merge 只可以作为xml layout的根节点。

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher" />


    <include layout="@layout/layout_merge" />

</LinearLayout>

R.layout.layout_merge

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="merge one" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="merge two" />
</merge>

这里写图片描述
在不影响层级深度的情况下,使用LinertLayout 而不是RelativeLayout,因为RelativeLayout会让子view调用两次onMeasure。LinearLayout在有weight时才会让子view调用2次onMeasure

viewstub

有的时候我们会遇到这样的场景,就是某个布局当中的元素非常多,但并不是所有元素都一起显示出来的,而是普通情况下只显示部分常用的元素,而那些不常用的元素只有在用户进行特定操作的情况下才会显示出来。
大多数人的第一反应就是将不常用的元素使用INVISIBLE或者GONE进行隐藏,然后当用户需要使用这些元素的时候再把它们置成VISIBLE显示出来。使用这种方式肯定可以实现功能的,但是性能方面就表现得一般了,因为即使是将元素进行隐藏,它们其实还是在布局当中的,每个元素还拥有着自己的宽、高、背景等等属性,解析布局的时候也会将这些隐藏的元素一一解析出来。
ViewStub虽说也是View的一种,但是它没有大小,没有绘制功能,也不参与布局,资源消耗非常低,将它放置在布局当中基本可以认为是完全不会影响性能的。


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {

            private TextView viewById1;
            private TextView viewById;

            @Override
            public void onClick(View v) {
                ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
                //对ViewStub的实例进行了一个非空判断,这是因为ViewStub在XML中定义的id只在一开始有效,一旦ViewStub中指定的布局加载之后,这个id也就失败了,那么此时findViewById()得到的值也会是空。
                if (viewStub != null) {
               // 拿到ViewStub的实例之后就很简单了,调用inflate()方法将隐藏的布局给加载出来
                    View inflatedView = viewStub.inflate();
                    //获取子元素的实例
                    viewById = ((TextView) inflatedView.findViewById(R.id.text));
                    viewById1 = ((TextView) inflatedView.findViewById(R.id.text2));

                }
            }
        });

    }

R.layout.activity_second

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ren00.mytestapplication.activity.SecondActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher" />

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/image"
        android:layout="@layout/view_stub_extra" />

</RelativeLayout>

R.layout.view_stub_extra

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:text="one one"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two two"/>

</LinearLayout>

这里写图片描述
这里写图片描述
ViewStub所加载的布局是不可以使用标签的,因此这有可能导致加载出来的布局存在着多余的嵌套结构,
使用是需要注意:
1.ViewStub 只能加载一次,之后ViewStub 对象会被置空。换句话说,某个被ViewStub指定的布局加载后,就不能通过ViewStub来控制它了。所以它不适用于按需要显示隐藏的情况。
2.ViewStub 只能用来加载一个布局文件,而不是某个具体的view。如果想操作某个具体的view还是要操作其visibility属性

提高布局方法总结

总体来说就是减少层级,提高绘制速度和布局的复用
影响布局效率的主要有以下几点:
布局层级越少,加载速度越快
减少同一层级的控件的数量,加载速度越快
尽量少用wrap_content,wrap_content 会增加布局measure时的计算成本,已知宽高为固定值时,不用wrap_content
删除控件中的无用属性

用textview显示文字和图片

<string name="name">abcd\nabcd\nabcd\n</string>

这里写图片描述

运用SpannableStringBuilder和SpannableString

SpannableString、SpannableStringBuilder基本上与String差不多,也是用来存储字符串,但它们俩的特殊就在于有一个SetSpan()函数,能给这些存储的String添加各种格式或者称样式(Span),将原来的String以不同的样式显示出来
SpannableString像一个String一样,构造对象的时候传入一个String,之后再无法更改String的内容,也无法拼接多个 SpannableString;而SpannableStringBuilder则更像是StringBuilder,它可以通过其append()方法来拼接多个String;


        SpannableStringBuilder ss=new SpannableStringBuilder("黄色,超链接,粗体,删除线,下划线,图片:.");
        //用颜色标记文本
        ss.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, 2,
                //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用超链接标记文本
        ss.setSpan(new URLSpan("tel:10086"), 3, 6,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用样式标记文本(粗体)
        ss.setSpan(new StyleSpan(Typeface.BOLD), 7, 9,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用删除线标记文本
        ss.setSpan(new StrikethroughSpan(), 10, 13,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用下划线标记文本
        ss.setSpan(new UnderlineSpan(), 14, 17,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //获取Drawable资源
        Drawable d = getResources().getDrawable(R.drawable.ic_launcher_background);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        //创建ImageSpan
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        //用ImageSpan替换文本
        ss.setSpan(span, 20, 21, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spanTv.setText(ss);

这里写图片描述

Space控件

如果给条目中间添加间距
①添加view :增加了view 增加了控件 ,影响性能
②使用layout_marginTop:使用过多的margin 影响代码的可读性
可用轻量级的替代

<Space
        android:layout_width="match_parent"
        android:layout_height="50dp" />
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值