設(shè)置背景顏色

兩種方法實(shí)現(xiàn)Activity透明/半透明效果的設(shè)置,代碼思路很有調(diào)理,感興趣的朋友可以參考下,希望本文可以幫助到你

方法一
res/values文件夾下建立styles.xml:
復(fù)制代碼
代碼如下:

<?xml version=“1.0″ encoding=“utf-8″?>
<style name="translucent">
<item name="android:windowBackground">@color/translucent_background</item>
<item name="android:windowIsTranslucent">true</item>
</style>

在該文件夾下在創(chuàng)建文件colors.xml
復(fù)制代碼
代碼如下:

<?xml version=“1.0″ encoding=“UTF-8″?>
<RESOURCES>
<color name="translucent_background">#60000000</color>
</RESOURCES>

有了這寫設(shè)置,就得告訴Activity用這寫設(shè)置.
AndroidManifest.xml中找到要彈出的activity,加入theme:
android:theme=”@style/translucent”
哎,不錯(cuò),確實(shí)透明了.但是問題又來了,layout里的button不透明啊.如果能讓他們也透明或者半透明呢?那得設(shè)置窗口屬性.
復(fù)制代碼
代碼如下:

Window window=getWindow();
WindowManager.LayoutParams wl = window.getAttributes();
wl.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
wl.alpha=0.6f;   這句就是設(shè)置窗口里崆件的透明度的.0.0全透明.1.0不透明.
window.setAttributes(wl);

方法二
今天試著做activity半透明的效果,做出來之后才發(fā)現(xiàn)想復(fù)雜了!很簡單的幾句就可以實(shí)現(xiàn),不多說了,貼代碼!
res/values/styles.xml
復(fù)制代碼
代碼如下:

<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
復(fù)制代碼
代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent_background">#50000000</color>
</resources>

注意:color.xml的#5000000前兩位是透明的效果參數(shù)從00--99(透明--不怎么透明),后6位是顏色的設(shè)置
manifest.xml
復(fù)制代碼
代碼如下:

<activity android:name=".TransparentActivity" android:theme="@style/Transparent">
</activity>

java代碼
復(fù)制代碼
代碼如下:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Transparent);
setContentView(R.layout.transparent);
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容