【安卓学习笔记】布局优化——include、merge、ViewStub标签的应用

效果图

效果图

要点:

  • 某个布局被其他布局多次引用时(如常见的标题栏),可使用Include直接引入。
  • 当某个布局中插入其他布局时,会引入多余的嵌套层,降低视图加载效率。此时可用merge标签,减少嵌套布局。
  • 使用ViewStub标签来加载一些不常出现的布局。如进度条、对话框等。ViewStub包含的布局在初始化时不会被加载。而在标签中加visibility=gone这种方法在初始化时也会加载。

源码

1、commom_title.xml,标题栏可被很多页面共用,这里先单独提出来。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#f15a22"
    android:padding="10dip" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="标题"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="功能"
        android:textSize="20sp" />

</RelativeLayout>

2、content.xml,这里使用了merge标签,在被引用时,merge标签所在层级被直接忽略

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示进度条"
        android:textSize="26sp"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dip"
        android:id="@+id/btn"/>   
</merge>

3、progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center">
    
     <ProgressBar 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/progress"
         style="?android:attr/progressBarStyleLarge"/>     

</LinearLayout>

4、activity_main.xml,通过Include引用标题栏。为了说明merge的用法,单独定义了一个button布局,其父层用merge标签,被Include进来后,merge父层被忽略,相当于button直接被添加到activity_main中。一般include和merge一起用。另外,使用了viewStub标签,把进度放进来。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"      
    tools:context=".MainActivity">

    <include layout="@layout/commom_title"/>
    
    <include layout="@layout/content"/>
    
    <ViewStub 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/progressbar"
        android:id="@+id/viewStub"/>

</LinearLayout>

5、MainActivity,初始化布局时不会加载ViewStub 中包含的内容。可节省内存。通过按钮触发显示ViewStub 中的进度。

public class MainActivity extends Activity{
    
    private ViewStub vs;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);
        
        vs = (ViewStub) findViewById(R.id.viewStub);
        btn = (Button) findViewById(R.id.btn);
        
        
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                vs.inflate();
            }
        });     
    }   
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容