亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android抽象布局——include、merge 、ViewStub詳解

 更新時(shí)間:2016年12月12日 16:08:28   作者:xyz_lmn  
這篇文章主要介紹了 Android抽象布局——include、merge 、ViewStub詳解,詳細(xì)的介紹了三種布局各有的優(yōu)勢(shì),有興趣的同學(xué)可以參考一下。

在布局優(yōu)化中,Androi的官方提到了這三種布局<include />、<merge />、<ViewStub />,并介紹了這三種布局各有的優(yōu)勢(shì),下面也是簡(jiǎn)單說一下他們的優(yōu)勢(shì),以及怎么使用,記下來權(quán)當(dāng)做筆記。

1、布局重用<include />

<include />標(biāo)簽?zāi)軌蛑赜貌季治募?,?jiǎn)單的使用如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width=”match_parent” 
 android:layout_height=”match_parent” 
 android:background="@color/app_bg" 
 android:gravity="center_horizontal"> 
 
 <include layout="@layout/titlebar"/> 
 
 <TextView android:layout_width=”match_parent” 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    android:padding="10dp" /> 
 
 ... 
 
</LinearLayout> 

1)<include />標(biāo)簽可以使用單獨(dú)的layout屬性,這個(gè)也是必須使用的。

2)可以使用其他屬性。<include />標(biāo)簽若指定了ID屬性,而你的layout也定義了ID,則你的layout的ID會(huì)被覆蓋,解決方案。

3)在include標(biāo)簽中所有的Android:layout_*都是有效的,前提是必須要寫layout_width和layout_height兩個(gè)屬性。

4)布局中可以包含兩個(gè)相同的include標(biāo)簽,引用時(shí)可以使用如下方法解決(參考):

View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite); 
 
bookmarks_container_2.findViewById(R.id.bookmarks_list); 

2、減少視圖層級(jí)<merge />

<merge/>標(biāo)簽在UI的結(jié)構(gòu)優(yōu)化中起著非常重要的作用,它可以刪減多余的層級(jí),優(yōu)化UI。<merge/>多用于替換FrameLayout或者當(dāng)一個(gè)布局包含另一個(gè)時(shí),<merge/>標(biāo)簽消除視圖層次結(jié)構(gòu)中多余的視圖組。例如你的主布局文件是垂直布局,引入了一個(gè)垂直布局的include,這是如果include布局使用的LinearLayout就沒意義了,使用的話反而減慢你的UI表現(xiàn)。這時(shí)可以使用<merge/>標(biāo)簽優(yōu)化。

 <merge xmlns:android="http://schemas.android.com/apk/res/android"> 
 
 <Button 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/add"/> 
 
 <Button 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/delete"/> 
 
</merge> 

現(xiàn)在,當(dāng)你添加該布局文件時(shí)(使用<include />標(biāo)簽),系統(tǒng)忽略<merge />節(jié)點(diǎn)并且直接添加兩個(gè)Button。更多<merge />介紹可以參考《Android Layout Tricks #3: Optimize by merging》

3、需要時(shí)使用<ViewStub />

<ViewStub />標(biāo)簽最大的優(yōu)點(diǎn)是當(dāng)你需要時(shí)才會(huì)加載,使用他并不會(huì)影響UI初始化時(shí)的性能。各種不常用的布局想進(jìn)度條、顯示錯(cuò)誤消息等可以使用<ViewStub />標(biāo)簽,以減少內(nèi)存使用量,加快渲染速度。<ViewStub />是一個(gè)不可見的,大小為0的View。<ViewStub />標(biāo)簽使用如下:

<ViewStub 
 android:id="@+id/stub_import" 
 android:inflatedId="@+id/panel_import" 
 android:layout="@layout/progress_overlay" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_gravity="bottom" /> 

當(dāng)你想加載布局時(shí),可以使用下面其中一種方法:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); 
// or 
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate(); 

當(dāng)調(diào)用inflate()函數(shù)的時(shí)候,ViewStub被引用的資源替代,并且返回引用的view。 這樣程序可以直接得到引用的view而不用再次調(diào)用函數(shù)findViewById()來查找了。

注:ViewStub目前有個(gè)缺陷就是還不支持 <merge /> 標(biāo)簽。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論