編輯:關於 Android 程式設計
Toast 的作用是向用戶彈出一條提示信息,停留片刻後自動消失,無需用戶點擊關閉:
這個名字的由來挺形象:烤麵包機把麵包烤好之後「騰」地一下彈出來,而這個控件的出現方式同樣是從底部彈出、短暫停留後消失,於是得名 Toast(吐司)。
Toast 有個很有用的特性:即使啟動它的 Activity 此刻並不在前台,提示也能照樣顯示在最上層。舉個典型場景:
Home 回到桌面,隨後又打開了另一個應用的 Activity BToast 提示用戶之所以能做到這一點,是因為 Toast 被顯示在一個特殊的窗口層次上,這個層次比任何 Activity 所使用的窗口都要更高,因而能夠優先覆蓋在上層。關於窗口系統的具體機制,會在後續講解 Android 窗口管理時再展開。
用法非常精簡:調用 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()
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>
另一個布局 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>
接著把 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 的價值就在於此:當你只是想引入一組子控件、並不需要額外多一層容器時,它能省掉一次無意義的嵌套,布局層級淺一層,測量和渲染的開銷也就跟著降下來。
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 一次,後續調用不會重複加載。
Handler 消息機制完全解析(二):MessageQueue 的隊列管理
關於這個隊列,先說明一點:它的實現既非 Collection 的子類,亦非 Map 的子類,而是 Message 本身。因為 Message 本身就是鏈結串列節點。隊列中
Android Fragment 通訊:Activity、Fragment 與 Fragment 之間
Fragment 之間的溝通,是 Android 開發中很常見的需求。最簡單的情況是 Activity 與 Fragment 傳值,進階一點則是 Fragment 與 F
Android 動畫入門(一):Drawable Animation 逐幀動畫完整解析
打算寫一系列文章來梳理 Android 的動畫體系,這一篇先從整體輪廓講起,再聚焦到最容易被理解的那一類。 Android 動畫的三大類 Android 的動畫大致可
Android 自訂 View 繪圖重點整理:Paint 與 Canvas
自訂 View 的核心是掌握 Paint(畫筆)與 Canvas(畫布)的用法。本文系統整理兩者最常見的設定與繪製方法。 一、Paint 的基本設定 paint.set