设置Activity为透明时只需要设置相应的Activitty主题即可,
android:theme="@android:style/Theme.Translucent"
但是当我们的Activity继承为AppCompatActivity时,这样设置就会出现这个异常:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
这是因为Appcompat中没有这些属性,所以我们需要自定义主题实现透明
<style name="TranslucentFullScreenTheme" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
这样在我们的Manifest文件中只需要给相应的Activity设置android:theme就可以了。
另外在我们的TranslucentFullScreenTheme加上这一行,就可以实现全屏了,也就是将工具栏隐藏掉。。。
<item name="android:windowFullscreen">true</item>
如有不对的地方请各位朋友指出,一起交流进步。