編輯:關於 Android 程式設計
「透明」在 Android 裡其實是好幾件不同的事:View 的背景透明、整個 Activity 變成半透明浮層、視窗本身的不透明度、以及背後畫面的變暗與模糊。搞混它們就會出現「設了半天沒效果」的情況。以下逐一釐清。
先繼承系統現成的半透明主題:
<style name="transparent2" parent="@android:style/Theme.Translucent">
<item name="android:windowNoTitle">true</item>
</style>
或自行組合各項屬性,控制更精細:
<style name="transparent">
<!-- windowIsFloating 會讓版面浮在螢幕中央,只適合 Dialog,一般 Activity 不要開 -->
<!-- <item name="android:windowIsFloating">true</item> -->
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
然後在 AndroidManifest.xml 中套用:
<activity
android:name=".TransparentActivity"
android:theme="@style/Transparent" />
或者在 Java 程式碼中設定(必須在 setContentView() 之前):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Transparent);
setContentView(R.layout.transparent);
}
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:textColor="#ffffff" />
Color 的格式是 #AARRGGBB,前兩位是 alpha:
<!-- 半透明黑 --> <Button android:background="#e0000000" /> <!-- 完全透明 --> <Button android:background="#00000000" />
alpha 從 00(全透明)到 ff(全不透明),e0 約為 88% 不透明度。
View v = findViewById(R.id.content); v.getBackground().setAlpha(100); // 0~255
注意這裡的數值範圍是 0~255,與 ARGB 的十六進位表示對應。
這與背景顏色無關,是整個視窗的 alpha 值:
WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.3f; getWindow().setAttributes(lp);
0.0f 完全透明,1.0f 完全不透明。設定後整個 Activity(包含所有子 View)都會變淡。
WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.dimAmount = 0.5f; getWindow().setAttributes(lp); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dimAmount 範圍 0.0f(不變暗)到 1.0f(全暗)。這正是 Dialog 預設會把背景變暗的效果,想拿掉就在主題裡設 android:backgroundDimEnabled 為 false。
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
這個 API 在 Android 12 之後已被標記為不再建议使用,且效能開銷不小。現代做法是用 RenderEffect.createBlurEffect()(API 31+)或 BlurView 這類第三方函式庫。
res/values/styles.xml
<resources>
<style name="Transparent">
<item name="android:windowBackground">
@color/transparent_background
</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">
@+android:style/Animation.Translucent
</item>
</style>
</resources>
res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent_background">#50000000</color>
</resources>
#50000000 中,前兩位 50 是 alpha(約 31% 不透明),後六位 000000 是顏色(黑)。調整前兩位就能控制蒙層的濃淡。
AndroidManifest.xml
<activity
android:name=".TransparentActivity"
android:theme="@style/Transparent">
</activity>
程式碼
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Transparent);
setContentView(R.layout.transparent);
}
Dialog 本來就是浮在 Activity 之上的視窗,這些 WindowManager 參數都能直接使用。常見做法是:
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
這樣可以移除 Dialog 預設的白色方形背景,讓自訂的圓角或異形版面正常呈現。
這四者是獨立的概念,可以組合使用,但也因此容易搞混。釐清「要透明的是 View、Activity 還是背後的東西」,就不會再試錯。
Android 觸摸事件分發與攔截機制完全解析
前言 在自訂 ViewGroup 中,觸摸事件攔截是很常見的需求。例如下拉刷新、側滑手勢或可拖曳面板,都需要父容器在特定時機攔截事件,避免子控制項搶走手勢。 事件分發的基
Android 自訂驗證碼倒數計時按鈕元件
先看效果: 設計思路 原理並不複雜:把倒數邏輯封裝進一個自訂檢視中,並對外暴露回呼介面,讓使用方決定「按下時要做什麼」與「計時中如何反應」。這樣一來,任何頁面要加驗證碼
ImageView 源碼透視:八種 ScaleType 的縮放規則一次講透
前言 ImageView 是 Android 裡最常打交道的控件之一。它看起來簡單,但真正決定「圖片怎麼擺」的,是那個經常被隨手填上的 scaleType。搞懂它背後的
Android ListView 單選效果實作:以 ImageView 模擬單選按鈕
做 Android 開發到現在有段時間了,技術也慢慢有了些積累,剛好把自己實現過的一個小功能記錄下來,若也能幫到路上的其他開發者,那也算是件好事。廢話不多說,先上圖。