天天看點

Activity背景的透明度效果

Activity背景的透明度效果

方法一:通過系統自帶的主題 Theme.Translucent

@android:style/Theme.Translucent  
@android:style/Theme.Translucent.NoTitleBar  
@android:style/Theme.Translucent.NoTitleBar.Fullscreen  
           

隻需要在Manifest中需要透明的Activity内設定theme為以上任意一個就可以了

<activity  
    android:name="com.xxx.xxx"   
    android:label="@string/app_name"  
    android:theme="@android:style/Theme.Translucent.NoTitleBar" > 

    <intent-filter>  
        <action android:name="android.intent.action.MAIN" />  
        <category android:name="android.intent.category.LAUNCHER" />  
    </intent-filter>  
</activity>  
           

方法二:自定義style

就像自定義Dialog的style一樣,在res-values-color.xml中添加透明顔色值:

<?xml version="1.0" encoding="UTF-8"?>  
<resources>  
    <color name="transparent_bg">#5000000</color>  
</resources>  
           

tip:注意:color.xml的#5000000前兩位是透明的效果參數從00–99(透明–不怎麼透明),後6位是顔色的設定 純透明可以簡寫:#000

在res-values-styles.xml中添加如下:

<style name="myTransparent">  
    <item name="android:windowBackground">@color/transparent_bg</item>  
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowIsTranslucent">true</item>  
    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>  
</style>  
           

在Manifest中中需要透明的Activity内設定theme為我們自定義的即可

或者在代碼中引用布局:

public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setTheme(R.style.Transparent); //寫在前面   
    setContentView(R.layout.xxx);   
}   
           

Activity中控件的透明度效果

在Activity背景透明的基礎上可以設定裡面的控件的透明度

Window window=getWindow();
WindowManager.LayoutParams wl = window.getAttributes();
wl.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
wl.alpha=f;  //這句就是設定視窗裡控件的透明度的,0.0全透明,1.0不透明.
window.setAttributes(wl);