Android开发中页面启动时黑屏或者白屏的原因及解决办法

产生原因

之所以会显示黑屏或者白屏是因为还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景

解决办法

设置style,在主题中加入以下属性:
<item name="android:windowIsTranslucent">true</item>
如果是进入app的欢迎页出现这种情况还可以设置一个默认的背景图如下:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@mipmap/splash</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

之后只需要在AndroidManifest.xml中设置改主题就可以了

常见的主题

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="Theme.Translucent" //透明背景
android:theme="Theme.Translucent.NoTitleBar" //透明背景并无标题
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景并无标题,全屏
android:theme="Theme.Panel " //面板风格显示
android:theme="Theme.Light.Panel" //平板风格显示

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容