这个demo演示了如何使用样式设置activity的半透明效果。
基本思路:
1.在res的values的目录下新建colors.xml,在其中设置窗体的背景为半透明:#e0000000
2.在style.xml中定义窗体的样式
3.在配置文件中为窗体设定样式。
本例中activity.xml和MainActivity都不需要做什么修改,最多你也就是需要把textView的显示文本修改一下,这里不再贴源码了。
colors.xml,在这里设置了activity的背景色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="translucent_background">#e0000000</drawable>
</resources>
style.xml中定义了我们需要的样式,在这里我们首先继承了系统的透明样式,然后设置窗体的背景色和前景色,设置窗体没有标题
<style name="Theme.translucent" parent="android:style/Theme.Translucent">
<item name="android:windowBackground">@drawable/translucent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
配置文件中使用样式
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>