实现Activity半透明的方式有很多,这里只列举其中的两种。
1、通过Theme.Translucent(不推荐)
2、自定义style
一、只需要在Manifest中将需要设置半透明的Activity设置为以下之一即可
@android:style/Theme.Translucent
@android:style/Theme.Translucent.NoTitleBar
@android:style/Theme.Translucent.NoTitleBar.Fullscreen
但是当Activity继承自AppcompatActivity时,会出现
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
可以这样写
<style name="transparent_">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
也可以这样写
<style name="translucent" parent="AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
同样在Manifest中设置Theme就可以了