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

Android 透明與半透明設定總整理:Style、ARGB、alpha 三條路

編輯:關於 Android 程式設計

「透明」在 Android 裡其實是好幾件不同的事:View 的背景透明、整個 Activity 變成半透明浮層、視窗本身的不透明度、以及背後畫面的變暗與模糊。搞混它們就會出現「設了半天沒效果」的情況。以下逐一釐清。

一、透過 Style 讓整個 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);
}

二、View 背景的透明:三種做法

方法 1:用系統內建的透明色

<Button
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:textColor="#ffffff" />

方法 2:用 ARGB 自行指定

Color 的格式是 #AARRGGBB,前兩位是 alpha:

<!-- 半透明黑 -->
<Button android:background="#e0000000" />

<!-- 完全透明 -->
<Button android:background="#00000000" />

alpha 從 00(全透明)到 ff(全不透明),e0 約為 88% 不透明度。

方法 3:程式碼設定 alpha

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 這類第三方函式庫。

六、完整的半透明 Activity 範例

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 同樣適用

Dialog 本來就是浮在 Activity 之上的視窗,這些 WindowManager 參數都能直接使用。常見做法是:

dialog.getWindow().setBackgroundDrawable(
        new ColorDrawable(android.graphics.Color.TRANSPARENT));

這樣可以移除 Dialog 預設的白色方形背景,讓自訂的圓角或異形版面正常呈現。

挑選指南

  • 只想讓某個 View 的背景消失 → ARGB 設為 #00000000,或 @android:color/transparent。
  • 想讓整個 Activity 變成浮層、能看到底下的畫面 → Style 設 windowIsTranslucent + windowBackground 半透明色。
  • 想讓整個視窗變淡 → WindowManager.LayoutParams.alpha。
  • 想讓背後變暗以聚焦注意力 → dimAmount + FLAG_DIM_BEHIND。

這四者是獨立的概念,可以組合使用,但也因此容易搞混。釐清「要透明的是 View、Activity 還是背後的東西」,就不會再試錯。

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