【安卓學習筆記】布局優化——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();
            }
        });     
    }   
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容