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爲我們自定義的即可

android:theme="@style/myTransparent"  

或者在代碼中引用佈局:

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=0.3f;  //這句就是設置窗口裏控件的透明度的,0.0全透明,1.0不透明.
window.setAttributes(wl);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章