布局优化

(一)使用include标签

(二)使用merge标签

注意事项:

  • 1,布局根节点是frameLayout,并且不需要设置backgroud或者padding等属性,
  • 2,某布局作为子布局被其他布局include时,使用merge当做该布局的顶节点,这样在被引入顶节点会自动被忽略

(三)使用viewStud,进行延时加载

  • 1,布局
    <Button
    android:id="@+id/delay_load"
    android:text="viewstub延时加载"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

    <ViewStub
    android:id="@+id/view_stub"
    android:layout="@layout/viewstub_test"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

  • 1.2viewstub加载的布局
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    android:text="延时加载的内容"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

    </LinearLayout>

  • 2,代码,点击button的时候,才加载延时内容

点击的时候才加载布局内容,避免占用内存,提高性能
  mViewStub = (ViewStub) findViewById(R.id.view_stub);
    findViewById(R.id.delay_load).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mViewStub.inflate();
        }
    });
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容