美文网首页
布局优化

布局优化

作者: 小毕_先生 | 来源:发表于2018-01-12 16:40 被阅读0次

    (一)使用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();
            }
        });

    相关文章

      网友评论

          本文标题:布局优化

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