編輯:關於 Android 程式設計
在開發應用時,我們經常會遇到這樣的情境:需要在執行階段根據條件,動態決定要顯示哪個 View 或某段版面配置。最直覺的做法,是把所有可能用到的 View 都寫進版面配置,先將它們的可見性設為 View.GONE,之後再在程式碼中動態切換。
這種做法的優點是邏輯簡單、控制靈活;但缺點在於浪費資源。即便初始可見性設為 View.GONE,在 Inflate 版面配置的過程中,這些 View 仍然會被建立、被實例化、被套用屬性,也就是說,它們照樣會佔用記憶體等系統資源。
更恰當的方式是使用 android.view.ViewStub。ViewStub 是一個極為輕量的元件,它本身看不見、不佔據版面配置位置,所消耗的資源微乎其微。你可以為 ViewStub 指定一個版面配置,在 Inflate 時只有 ViewStub 本身會被初始化;直到 ViewStub 被設為可見,或呼叫了 ViewStub.inflate() 時,它所指向的版面配置才會真正被 Inflate 與實例化,同時 ViewStub 的版面配置屬性也會一併傳遞給目標版面配置。如此一來,就能在執行階段輕鬆決定某段版面配置要不要顯示。
不過 ViewStub 並非萬能,以下整理它的幾個關鍵特點:
1. ViewStub 只能 Inflate 一次,之後 ViewStub 物件會被置空。也就是說,某個被 ViewStub 指定的版面配置一旦被 Inflate,就無法再透過 ViewStub 來控制它了。
2. ViewStub 只能用來 Inflate 一個版面配置檔,而非某個具體的 View(當然,你也可以把 View 寫進某個版面配置檔裡)。
基於上述特點,以下情況很適合採用 ViewStub:
1. 在程式執行期間,某個版面配置一旦被 Inflate 就不會再有變化(除非重新啟動)。因為 ViewStub 只能 Inflate 一次且隨即被置空,若需要在執行階段反覆顯示與隱藏某個版面配置,ViewStub 就力有未逮,這時只能改用 View 的可見性來控制。
2. 想要控制顯示與隱藏的是一整個版面配置檔,而非單一 View。由於指定給 ViewStub 的只能是版面配置檔的 Id,因此無法讓它控制單獨的某個 View。若你需要控制某個 Button 或 TextView 的顯示與隱藏,或需要在執行階段不斷切換,就只能仰賴 View 的可見性。
以下範例要展示兩種不同的版面配置:一個用 TextView 顯示一段文字,另一個用 ImageView 顯示一張圖片,並在 onCreate() 時決定要顯示哪一個——這正是 ViewStub 最能發揮所長的場景。
主版面配置只定義兩個 ViewStub,分別控制文字與圖片:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<ViewStub
android:id="@+id/viewstub_demo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip"
android:layout="@layout/viewstub_demo_text_layout"/>
<ViewStub
android:id="@+id/viewstub_demo_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout="@layout/viewstub_demo_image_layout"/>
</LinearLayout>
文字專用的版面配置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/viewstub_demo_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#aa664411"
android:textSize="16sp"/>
</LinearLayout>
圖片專用的版面配置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/viewstub_demo_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
程式碼部分,只需找到對應的 ViewStub 並呼叫它的 inflate(),就能取得想要的版面配置:
package com.effective;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewStubDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewstub_demo_activity);
if ((((int) (Math.random() * 100)) & 0x01) == 0) {
// to show text
ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);
stub.inflate();
TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);
text.setText("The tree of liberty must be refreshed from time to time"
+ " with the blood of patroits and tyrants! Freedom is nothing but "
+ "a chance to be better!");
} else {
// to show image
ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);
stub.inflate();
ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);
image.setImageResource(R.drawable.happy_running_dog);
}
}
}
執行結果:


某些版面配置屬性必須加在 ViewStub 上,而非實際的版面配置上,才能生效。以範例中的 android:layout_margin* 系列屬性為例,若加在 TextView 上並不會起作用,必須放在它的 ViewStub 上才行。而 ViewStub 的屬性會在 inflate() 後全部傳遞給對應的版面配置。
Android Studio 開啟專案龜速卡關?兩招擺脫 Gradle 下載地獄
Android Studio 天生就靠 Gradle 撐起整個建置流程,並用 Groovy 這門動態語言來描述打包過程,比起過去那套又臭又長的 build.xml,確實清
Android 調試時出現 The selected device is incompatible 的解決方式
下圖為裝置不相容時常見的錯誤畫面。 可能原因 這個問題通常有兩種常見原因。第一是 App 的版本設定與裝置系統版本不相容,例如 minSdkVersion 高於裝置可支
自定義一個會提示剩餘字數的 EditText
最近專案有個需求:即時提示使用者還能打幾個字,也就是要監聽 EditText 的文字變化。功能本身不難,但每次都用組合方式重寫一遍實在麻煩,於是就把它包成一個自定義控件,
RecyclerView 實作複雜首頁布局的三種方式
電商、影音與內容平台的首頁,經常需要同時呈現橫幅、列表、網格與瀑布流。本文以 RecyclerView 為核心,整理三種常見實作方式,並比較其適用性。 方案一:Rec