星期日, 6月 12, 2011

如何在 Android 裡製作一個透明背景的 Activity?

方法一:(完全透明)

只要在 AndroidManifest.xml 中,為該 Activity 加上一行「android:theme="@android:style/Theme.Translucent"」即可。
<activity android:name=".ProgressAdMob"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent"
    android:screenOrientation="portrait"
    android:configChanges="orientation">
</activity>

方法二:(自定透明程度)
  • 建立檔案:res/values/styles.xml,其中那個 #aa000000 的前兩位就是指定透明程度(00:最透明,ff:最不透明)
  • <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="custom_theme_color">#aa000000</color>
        <style name="Theme.Transparent" parent="android:Theme">
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowBackground">@color/custom_theme_color</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:backgroundDimEnabled">false</item>
        </style>
    </resources>
    
  • 在 AndroidManifest.xml 中,為該 Activity 加上一行「android:theme="@android:style/Theme.Transparent"」即可。
  • <activity android:name=".ProgressAdMob"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Transparent"
        android:screenOrientation="portrait"
        android:configChanges="orientation">
    </activity>
    
[Ref]:
http://developer.android.com/guide/topics/ui/themes.html 

0 意見: