安卓性能优化之布局优化

< include>标签的使用。

   用法实例:
   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.shinelon.maptestactivity.Main2Activity">

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

</LinearLayout>
  用法解说:
  @layout/reuse:就是我们所引用过来的复用布局。
  这里注意的是,当要给 <include layout="@layout/reuse" />里面添加属性时,
  必须把android:layout_width 和android:layout_height写上,否则不起作用(如果
  这里写id属性就这里为准,并且其他属性必须是android:layout_ 开头的属性)
  例如:
  <include
    android:id="@+id/ic_reuse"
    layout="@layout/reuse"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

< merge>标签的使用。

解说:
上面布局是用LinearLayout的垂直布局,如果里面的布局也就是@layout/reuse这个布局
也采用了竖直方向的LinearLayout,则里面就可以直接用<merge>标签从而来减少布局的层级。

例如修改后(里面的布局@layout/reuse):

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="公用布局1"
    android:textSize="36sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="公用布局2"
    android:textSize="36sp" />
</merge>

ViewStub 的使用。

  解说:
  ViewStub继承了View,是一个轻量级且宽高都为0,因此本身不参与任何布局和绘制过程。
  ViewStub的意义就是按需加载所需要的界面,在实际开发中,有很多布局文件在正常情况下也要隐藏,
  比如网络异常,就没必要去初始化时加载进来了,通过他就可以在界面使用的时候在加载,提高初始化
  的性能。

  例如:

  <ViewStub
    android:id="@+id/vs_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inflatedId="@+id/root_id"
    android:layout="@layout/root" />

解说:
id 自然就是ViewStub的id了。
inflatedId是@layout/root里的id
使用:
在使用这个ViewStub时,有两种方式:
1、 ((ViewStub) findViewById(R.id.vs_id)).setVisibility(View.VISIBLE);

2、   View vs = ((ViewStub) findViewById(R.id.vs_id)).inflate();

当用上面两种方法加载后,ViewStub就会被内部的布局@layout/root替换掉,这个时候ViewStub就不再是布局中的一部分了。
另外,ViewStub暂时不支持<merge>标签。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若♡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值