采用透明主题的Activity一定要继承Activity,不然会崩溃。
我就是最开始就忘了这个了,然后导致崩溃,我还想了半天,继承我自己写的父类,哎。。。一着急啥都忘了。
一般会有两种方式实现
- 第一种是直接粗暴的在你的AndroidManifest里的Activity标签里配置透明主题:
android:theme="@android:style/Theme.Translucent"
其实系统已经定义了好多的样式可供大家选择,下面罗列一些:
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏 android:theme="Theme.Light" 背景为白色 android:theme="Theme.Light.NoTitleBar" 白色背景并无标题栏 android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏 android:theme="Theme.Black" 背景黑色 android:theme="Theme.Black.NoTitleBar" 黑色背景并无标题栏 android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏 android:theme="Theme.Wallpaper" 用系统桌面为应用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏 android:theme="Translucent" 透明背景 android:theme="Theme.Translucent.NoTitleBar" 透明背景并无标题 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 透明背景并无标题,全屏 android:theme="Theme.Panel" 面板风格显示 android:theme="Theme.Light.Panel" 平板风格显示
-
第二种也是比较简单的,那就是自己自定义了
首先就是在style.xml里面自定义主题样式<style name="MyTransparent" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="android:windowBackground">@color/translucent_background</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> </style>
然后在manifest里面进行引用就好
<activity android:name=".ui.Activity_Message" android:theme="@style/MyTransparent" />
其中:colorPrimaryDark 这个是状态栏的颜色,一般也是要和你的保持一致的;android:windowBackground 这个才是最主要的,这个决定了背景色会是什么样的,我的颜色是#00000000 这个表示完全透明的意思,效果好点,第一个数值往上加就会慢慢的变灰,喜欢的朋友可以自己试一下;android:windowAnimationStyle 这是系统的显示的动画,可以用自己的,我的是默认的。
最后提醒大家注意一点的是,设置之后,在布局文件里面,最外面的布局最好就不要设置背景色了,不然这个透明效果就没有了