Android app启动时黑屏或者白屏的原因及解决办法

作者:lqh 时间:2023-06-09 11:32:32 

1、产生原因

其实显示黑屏或者白屏实属正常,这是因为还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。

示例:

Android app启动时黑屏或者白屏的原因及解决办法

2、解决办法

通过设置设置Style

(1)设置背景图Theme

通过设置一张背景图。 当程序启动时,首先显示这张背景图,避免出现黑屏


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

(2)设置透明Theme

通过把样式设置为透明,程序启动后不会黑屏而是整个透明了,等到界面初始化完才一次性显示出来


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:windowIsTranslucent">true</item>
   <item name="android:screenOrientation">portrait</item>
 </style>

两者对比:

Theme1 程序启动快,界面先显示背景图,然后再刷新其他界面控件。给人刷新不同步感觉。
Theme2 给人程序启动慢感觉,界面一次性刷出来,刷新同步。 

(3)修改AndroidManifest.xml


<application
   android:name=".App"
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:supportsRtl="true">
   <activity android:name=".MainActivity"
    android:theme="@style/AppTheme">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
   </activity>

//......

</application>

解决后示例:

Android app启动时黑屏或者白屏的原因及解决办法

3、常见的Theme主题

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" //平板风格显示

标签:Android,app启动,黑屏,白屏
0
投稿

猜你喜欢

  • javaweb 项目初始配置的方法步骤

    2021-12-23 17:34:23
  • Spring5学习之基础知识总结

    2021-08-16 02:19:15
  • Android中把bitmap存成BMP格式图片的方法

    2022-10-02 10:09:16
  • 一键移除ButterKnife并替换为ViewBinding的旧项目拯救

    2023-08-19 12:30:28
  • 关于QueryWrapper,实现MybatisPlus多表关联查询方式

    2021-10-15 07:24:59
  • Android 听筒模式的具体实现实例

    2023-09-24 03:13:01
  • C#实现查杀本地与远程进程的方法

    2023-08-10 08:01:30
  • 浅析C++字节对齐容易被忽略的两个问题

    2022-06-02 20:29:59
  • Intellij IDEA 的maven项目通过Java代码实现Jetty的Http服务器(推荐)

    2022-02-19 20:29:13
  • Java中使用内存映射实现大文件上传实例

    2022-01-16 05:02:16
  • Android 高版本API方法在低版本系统上的兼容性处理

    2022-08-22 08:51:53
  • 浅谈Maven的安装及修改为阿里云下载依赖

    2023-08-05 08:30:37
  • java简单列出文件夹下所有文件的方法

    2022-12-23 19:25:05
  • java用接口、多态、继承、类计算三角形和矩形周长及面积的方法

    2021-10-24 22:15:46
  • Android开发入门之Appwidget用法分析

    2023-10-26 12:04:46
  • 解决JTable排序问题的方法详解

    2023-02-07 08:53:42
  • Android客户端程序Gradle如何打包

    2023-06-05 14:10:07
  • C#在子线程中更新窗口部件的写法

    2022-04-01 09:57:50
  • 基于JWT.NET的使用(详解)

    2021-07-09 22:15:25
  • java 使用foreach遍历集合元素的实例

    2022-11-17 09:24:58
  • asp之家 软件编程 m.aspxhome.com