美文网首页
布局优化include, viewstub, merge

布局优化include, viewstub, merge

作者: MC_Honva | 来源:发表于2017-03-01 10:41 被阅读32次
布局优化
1.include

视图引入,可以配合merge使用
重用布局

2.merge

减少视图层级
步骤如下:
1、创建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">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="123" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="123" />
</merge>

2、视图引入

<?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_main"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.ticker.myapplication.MainActivity">
    <include layout="@layout/merge_layout"></include>
</LinearLayout>
3.ViewStub

引入一个外部布局,不同的是,viewstub引入的布局默认不会扩张,即既不会占用显示也不会占用位置,从而在解析layout时节省cpu和内存。ViewStub是一个不可见的,大小为0的视图,可以在运行过程中延时加载布局资源,inflate 方法只能被调用一次,因为调用后viewStub对象就被移除了视图树;
使用步骤如下:
1、创建需要加载的布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:text="button01"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="match_parent"
        android:text="button02"
        android:layout_height="wrap_content" />
</LinearLayout>

2、在需要引入的xml中引入

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ticker.myapplication.MainActivity">
    <ViewStub
        android:id="@+id/viewstub"
        android:layout_width="match_parent"
        android:layout="@layout/viewstub_layout"
        android:layout_height="match_parent" />
</RelativeLayout>

3、使用

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewStub viewStub = (ViewStub) findViewById(R.id.viewstub);
        View view = viewStub.inflate();
    }
}

Android抽象布局——include、merge 、ViewStub
ViewStub--使用介绍

相关文章

  • Android-性能优化

    应用体验-布局优化 使用include布局、merge标签、ViewStub视图可以使用HierarchyView...

  • Android 性能优化

    布局优化 include 标签 比如导航栏merge 标签 减少布局的层级viewstub 继承view 本身不...

  • android应用性能优化

    1. UI布局的优化 使用include,merge,ViewStub标签优化布局 尽量不存在冗余嵌套及过于复杂的...

  • Android 知识

    Android 知识随笔 1.布局优化include merge viewstub 2视频播放流程 采集 —>处理...

  • Android 性能优化(UI渲染)

    注意事项: 布局优化;尽量使用include、merge、ViewStub标签,尽量不存在冗余嵌套及过于复杂布局(...

  • Android面试宝典 - 优化篇

    一、性能优化 1. 布局优化 尽量使用include,merge,ViewStub标签避免冗余嵌套以及过于复杂的布...

  • Android 性能优化

    布局优化 include 标签。 merge 标签。 ViewStub 视图。 减少视图绘制:1.尽量避免在列表布...

  • Android 性能优化

    布局优化 include 标签。 merge 标签。 ViewStub 视图。 减少视图绘制:1.尽量避免在列表布...

  • Android抽象布局——include、merge 、View

    Android抽象布局——include、merge 、ViewStub - CSDN博客

  • 布局优化include, viewstub, merge

    1 include 视图引入,可以配合merge使用重用布局 2.merge 标签在UI的结构优化...

网友评论

      本文标题:布局优化include, viewstub, merge

      本文链接:https://www.haomeiwen.com/subject/bsjqgttx.html