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

Android开发 -- 实现 Activity 的透明效果

2016-11-28 12:00 375 查看
方法一:

1 在 res/values/color.xml 文件下加入一个透明颜色值,这里的 color 参数,是两位数一个单位,前两位数是透明度(16进制:00 -- FF,最大为256,数值越低越透明),后面每两位一对是16进制颜色数字,示例中为白色。

<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="translucent_background">#80000000</color>

</resources>


2 在 res/values/styles.xml 文件中加入一个自定义样式,代码如下。

<!-- item name="android:windowBackground"         设置背景透明度及其颜色值 -->
<!-- item name="android:windowIsTranslucent"      设置当前Activity是否透明-->
<!-- item name="android:windowAnimationStyle"     设置当前Activity进出方式-->
<style name="translucent">
<item name="android:windowBackground">@color/translucent_background</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>

如若你的布局xml文件有
support-V7 上的控件的话,<style name="translucent">里的name要前要添加 AppTheme,如:<style name=" AppTheme.translucent">

【否则会报出这种错误:You need to use a Theme.AppCompat theme (or descendant) with the design library.】

3 在 AndroidManifest.xml 找到要实现透明的 Activity,在想要实现透明的 Activity 中配置其属性,如下:

<activity
android:name="cn.sunzn.transact.TouActivity"
android:theme="@style/translucent" >
</activity>


方法二:

1:在Activity的布局xml的根标签中写入透明颜色:

android:background="#80000000"


2:在 AndroidManifest.xml 找到要实现透明的 Activity,在想要实现透明的 Activity 中配置其属性,如下:

android:theme="@android:style/Theme.Translucent.NoTitleBar"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Activity透明