博客
关于我
Android 布局优化之<include/><merge/>和 <ViewStub>
阅读量:622 次
发布时间:2019-03-13

本文共 1956 字,大约阅读时间需要 6 分钟。

Excellent, let's optimize the provided content according to your requirements.

By marking up a layout for reuse in Android development, developers often need to reuse complex components like title bars. This can be efficiently achieved by creating a reusable layout file called include_toolbar.xml, which contains a Toolbar widget with a TextView inside it.

Here’s an example of the include_toolbar.xml layout file:

Now, in your activity_main, you can include this toolbar layout using the <include> tag:

When using the <include> tag in your layout, here are some important points to keep in mind:

  • The <include> tag’s id attribute will override the id attribute of the included layout’s root node.
  • The <include> tag’s layout_* attributes will override the included layout’s root node’s layout_* attributes.
  • In ConstraintLayout, using the <include> tag requires the included view to have layout_width and layout_height attributes defined.
  • To reduce the layout hierarchy levels, you can use the <merge> tag when including views. This helps in avoiding unnecessary nested layouts, which can affect performance:

    When this view is included in another layout, the <merge> tag will be ignored, and the buttons will be placed directly in the布局.

    If you need to load a layout only when it’s actually required, you can use the <ViewStub> tag. This allows you to defer the loading of these layouts until they are needed:

    The ViewStub layout can be inflated when needed using either:

    val errorView = findViewById
    (R.id.view_stub).inflate()

    It’s important to note that inflate() can only be called once. After the first call, the ViewStub will no longer be part of the view hierarchy, and its original id will only be valid until the layout is inflated.

    By following these principles, you can efficiently manage layout reuse and optimize your Android development workflow.

    转载地址:http://meoaz.baihongyu.com/

    你可能感兴趣的文章
    Nginx面试三连问:Nginx如何工作?负载均衡策略有哪些?如何限流?
    查看>>
    Nginx高可用集群构建(Keepalived+Haproxy+Nginx)
    查看>>
    Nginx(1):Nginx基础配置详解
    查看>>
    Nginx(2):Nginx配置server节点
    查看>>
    nginx:/usr/src/fastdfs-nginx-module/src/common.c:21:25:致命错误:fdfs_define.h:没有那个文件或目录 #include
    查看>>
    Nginx:NginxConfig可视化配置工具安装
    查看>>
    Nginx:现代Web服务器的瑞士军刀 | 文章末尾送典藏书籍
    查看>>
    ngModelController
    查看>>
    ngnix配置文件
    查看>>
    ngrok | 内网穿透,支持 HTTPS、国内访问、静态域名
    查看>>
    ngrok内网穿透可以实现资源共享吗?快解析更加简洁
    查看>>
    ngrok内网穿透可以实现资源共享吗?快解析更加简洁
    查看>>
    NHibernate动态添加表
    查看>>
    NHibernate学习[1]
    查看>>
    NHibernate异常:No persister for的解决办法
    查看>>
    Nhibernate的第一个实例
    查看>>
    NHibernate示例
    查看>>
    nid修改oracle11gR2数据库名
    查看>>
    NIFI1.21.0/NIFI1.22.0/NIFI1.24.0/NIFI1.26.0_2024-06-11最新版本安装_采用HTTP方式_搭建集群_实际操作---大数据之Nifi工作笔记0050
    查看>>
    NIFI1.21.0_java.net.SocketException:_Too many open files 打开的文件太多_实际操作---大数据之Nifi工作笔记0051
    查看>>