Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

布局與控件(五)——Toast 吐司與抽象標簽 merge、include、ViewStub

編輯:關於 Android 程式設計

第5節 Toast

5.1 使用效果

Toast 的作用是向用戶彈出一條提示信息,停留片刻後自動消失,無需用戶點擊關閉:

這個名字的由來挺形象:烤麵包機把麵包烤好之後「騰」地一下彈出來,而這個控件的出現方式同樣是從底部彈出、短暫停留後消失,於是得名 Toast(吐司)。

Toast 有個很有用的特性:即使啟動它的 Activity 此刻並不在前台,提示也能照樣顯示在最上層。舉個典型場景:

  • Activity A 正在後台下載文件
  • 用戶按下 Home 回到桌面,隨後又打開了另一個應用的 Activity B
  • 此時 Activity A 已經不可見
  • 下載完成後,隱藏在背後的 Activity A 調用 Toast 提示用戶
  • 儘管 A 沒有顯示,這條提示仍會浮在整個界面的最上層被用戶看到

5.2 顯示原理

之所以能做到這一點,是因為 Toast 被顯示在一個特殊的窗口層次上,這個層次比任何 Activity 所使用的窗口都要更高,因而能夠優先覆蓋在上層。關於窗口系統的具體機制,會在後續講解 Android 窗口管理時再展開。

5.3 使用方式

用法非常精簡:調用 Toast.makeText() 得到一個 Toast 對象,再調用它的 show() 讓它真正顯示出來。注意只 makeText 而不 show,是什麼都不會出現的,這是新手常犯的疏漏。

makeText 接受三個參數:

  • 第一個是 Context,代表當前 Activity 運行時所處的上下文環境,也就是它可訪問的各類系統資源。由於 Activity 本身就派生自 Context,所以直接傳 Activity 即可。
  • 第二個是要展示的文本內容。
  • 第三個是顯示時長,但注意它只有兩個可選值:Toast.LENGTH_SHORT 與 Toast.LENGTH_LONG,並不能傳任意毫秒數。
 Toast.makeText(context, "需要顯示的內容", Toast.LENGTH_SHORT).show()

第6節 布局的抽象標簽——merge、include、ViewStub

include、merge 與 ViewStub 都不對應任何具體的布局或控件,它們是輔助性的抽象標簽,用來提升布局的複用性與渲染效率。

最典型的場景是:某段布局結構會被反覆使用,那就把它單獨抽到一个 layout 文件裡,需要時再引進來,避免到處複製粘貼。

假設下面這段結構頻繁出現,我們把它獨立到 reuse.xml:

 <framelayout android:layout_height="match_parent" android:layout_width="match_parent"> <textview android:layout_height="match_parent" android:layout_width="match_parent"> </textview></framelayout>

6.1 include 標簽

另一個布局 main_layout.xml 若要複用這段結構,用 <include> 標簽,並在 layout 屬性中指定要引入的布局資源即可:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <!--包含復用的布局--> <include layout="@layout/reuse" /> </linearlayout>

那麼 main_layout.xml 在效果上就等同於:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <!--include的內容開始--> <framelayout android:layout_height="match_parent" android:layout_width="match_parent"> <textview android:layout_height="match_parent" android:layout_width="match_parent"> </textview></framelayout> <!--include的內容結束--> </linearlayout>

6.2 merge 標簽

接著把 reuse.xml 的外層容器從 FrameLayout 換成 merge:

 <merge> <textview android:layout_height="match_parent" android:layout_width="match_parent"> </textview></merge>

main_layout.xml 的引用方式不變:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <!--包含復用的布局--> <include layout="@layout/reuse" /> </linearlayout>

此時展開後的實際結構變成了:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <!--include的內容開始,少了FrameLayout一層--> <textview android:layout_height="match_parent" android:layout_width="match_parent"> <!--include的內容結束--> </textview></linearlayout>

與之前相比,FrameLayout 那一層包裹不見了。merge 的價值就在於此:當你只是想引入一組子控件、並不需要額外多一層容器時,它能省掉一次無意義的嵌套,布局層級淺一層,測量和渲染的開銷也就跟著降下來。

6.3 ViewStub 標簽

ViewStub 是一個佔位符。布局加載時它不會把自己聲明的內容真正創建出來,只有等到需要的那一刻,才由代碼觸發加載。對於那些平時用不上、只在特定條件下才出現的控件(比如加載中的進度條、錯誤提示塊),用它可以省下可觀的啟動開銷。

先看常規做法。布局 main_layout.xml 裡放一個 ProgressBar,那麼布局一被 inflate,這個 ProgressBar 就會跟著被創建:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <textview android:layout_height="wrap_content" android:layout_width="match_parent" android:text="this is a test"> <!--布局被加載時,這個ProgressBar就會被創建出來--> <progressbar android:layout_height="wrap_content" android:layout_width="wrap_content"> </progressbar></textview></linearlayout>

現在把 ProgressBar 拆到獨立文件 progress_layout.xml:

 <!--單獨放到progress_layout.xml當中--> <progressbar android:layout_height="wrap_content" android:layout_width="wrap_content"></progressbar>

再在 main_layout.xml 裡用 ViewStub 引用它:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <textview android:layout_height="wrap_content" android:layout_width="match_parent" android:text="this is a test"> <!--布局被加載時,這個ViewStub不會被創建出來--> <viewstub android:id="@+id/viewstub_id" android:inflatedId="@+id/progress_bar" android:layout="@layout/progress_layout" android:layout_height="wrap_content" android:layout_width="wrap_content"> </viewstub></textview></linearlayout>

此時 ProgressBar 並不會被創建。等到真的要用,通過兩種方式之一把 ViewStub 替換成 android:layout 指定的布局:

 ((ViewStub) findViewById(R.id.viewstub_id)).setVisibility(View.VISIBLE);
 ((ViewStub) findViewById(R.id.viewstub_id)).inflate();

替換完成後,ViewStub 自身會被從布局樹中移除,取而代之的是真正的內容:

 <linearlayout android:layout_height="match_parent" android:layout_width="match_parent"> <textview android:layout_height="wrap_content" android:layout_width="match_parent" android:text="this is a test"> <!--布局被加載時,這個ProgressBar就會被創建出來--> <progressbar android:inflatedId="@+id/progress_bar" android:layout_height="wrap_content" android:layout_width="wrap_content"> </progressbar></textview></linearlayout>

還有一點值得記住:ViewStub 天生不可見且不含尺寸,因此它的 layout_height、layout_width 等屬性只有在被 inflate 之後才會轉交給替換上來的控件生效;同一個 ViewStub 也只會被 inflate 一次,後續調用不會重複加載。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved