您的位置:首页 > 移动开发 > Android开发

Android Activity背景半透明设置方法

2012-09-11 09:45 399 查看
方法一:

一直在应用程序中使用的Activity都是黑色不透明的背景,很少遇到那些花哨的Activity,但是花哨归花哨,有些Activity一定要实现半透或者全透效果,比如我的一张动态壁纸的设置界面,这张设置界面是一张Activity页面,我在设置界面进行壁纸设置的时候,我希望看到我的壁纸已经作出相应的改变,为此就需要实现Activity的半透或者全透的效果了,那么如何在Android中实现Activity半透明效果或者全透明效果?

Activity实现全透明效果

Activity实现全透明效果方法,我们可以从sdk的帮助文章中找到方法,系统已经给我们做好了全透效果的style,所以我们只需在AndroidManifest.xml中声明该Activity时,添加上红色代码设置即可:

<activity android:name="org.ourunix.android.FreneticActivity"
android:icon="@drawable/small_pic.png"
android:label="@string/freneticLabel"
android:style="@android:style/Theme.Translucent"
. . .  >
</activity>


Activity实现半透明效果

如果我们要实现半透效果又该怎么办呢?那就需要我们自己写style了,但是这个style很简单,我们只需继承上面全透的系统style,然后加上背景色即可,步骤如下。

1、先在res/values下建colors.xml文件,写入:
<?xml version = "1.0" encoding = "UTF-8" ?>
<resources>
<color name = "transparent">#9000</color>
</resources>


2、这个值设定了整个界面的透明度,为了看得见效果,现在设为透明度为56%(9/16)左右。再在res/values/下建styles.xml,设置程序的style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Transparent">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
</resources>


3、最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任意<activity>标签中添加,同上。

<activity android:name="org.ourunix.android.FreneticActivity"
android:icon="@drawable/small_pic.png"
android:label="@string/freneticLabel"
android:style="@android:style/Transparent "
. . .  >
</activity>


附加:如果想设置所有的activity都使用这个风格,可以把这句标签语句添加在<application>中,最后运行程序,是不是发现整个界面都被蒙上一层半透明了。最后可以把背景色#9000换成#0000,运行程序后,就全透明了,看得见背景下的所有东西可以却都操作无效。

方法二:
设置背景半透明首先要更改AndroidManifest.xml中的主题属性
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SetWindow2Activity"
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>
</application>


然后在values/目录下新建style.xml文件 (注意新建时候,选择New → Other,然后选择android Xml File,如果直接新建文件(New → file)会提示出现错误)
然后在style.xml中 输入
<?xml version="1.0" encoding="utf-8"?>
<resources>
<stylename="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>
</resources>


style name属性Theme.Translucent对应AndroidManifest.xml中activity的android:theme="@style/Theme.Translucent">
第二个parent属性表示继承,也就是继承了系统资源@android:style/Theme.Translucent。

<item name="android:windowBackground">@drawable/translucent_background</item>设置背景颜色

在values目录下,新建colors.xml 内容如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="translucent_background">#5500ff</drawable>
</resources>


<drawable name="translucent_background">#5500ff</drawable>对应style.xml中的@drawable/translucent_background
这里颜色用32位图来表示,也就是alpha+RGB,前两位表示alpha通道取值00~ff之间,00为透明,ff为不透明,可根据需要自行设置;后面6位表示RGB颜色取值同样是00~ff(颜色由深到浅)
完成 运行如图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: