你会很奇怪,为什么有些app启动时,会出现一会儿的黑屏或者白屏才进入Activity的界面显示,但是有些app却不会如QQ手机端,的确这里要做处理一下。这里先了解一下为什么会出现这样的现象,其实很简单,简历一个简单的例子就可以理解了。
其实,黑屏或者白屏这里并不是不正常,而是还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。代码如下,可以自己写个小demo就理解了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 注意:添加3秒睡眠,以确保黑屏一会儿的效果明显,在项目应用要去掉这3秒睡眠
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 在这里之前,黑屏或者白屏都是window的背景颜色,是窗口背景,还没到界面的布局呢,要执行setContentView后才显示布局
setContentView(R.layout.activity_launcher);
}
那window窗口背景在那里提供呢?在提供theme里面,如下提供的是白色背景,那就是启动时白屏一会儿的颜色设置。
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/white</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
所以,在theme设置windowBackground就可以解决启动时白屏黑屏一会儿了,下面提供三种解决方案:
一、提供.png背景图
提供背景图是解决的一个方法,但是要适配各种屏幕,提供很多张图片。除非图片非常复杂只能用背景图了就用这种方法吧,否则个人不建议。
二、提供.9.png(NinePatch)背景图片
如果图片不是很复杂,可以做成NinePatch图片,那就直接制作NinePatch图片,提供一张就可以适配任何手机,何乐而不为呢。
三、使用Layout-list制作背景图片
如果可以使用这种方式,推荐使用这种Layout-list制作背景图片。前2种都是使用图片占用内存啊,使用Layout-list比较省内存,做出app也不会说因为图片多体积变大吧。
下面给出代码。
LaunchActivity为启动界面停留3秒后跳转到主页面MainActivity,为了达到显示黑屏白屏的效果更明显,在setContentView之前线程睡眠3秒。
public class LauncherActivity extends Activity {
public final int MSG_FINISH_LAUNCHERACTIVITY = 500;
public Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FINISH_LAUNCHERACTIVITY:
//跳转到MainActivity,并结束当前的LauncherActivity
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 不显示系统的标题栏,保证windowBackground和界面activity_main的大小一样,显示在屏幕不会有错位(去掉这一行试试就知道效果了)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 注意:添加3秒睡眠,以确保黑屏一会儿的效果明显,在项目应用要去掉这3秒睡眠
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_launcher);
// 停留3秒后发送消息,跳转到MainActivity
mHandler.sendEmptyMessageDelayed(MSG_FINISH_LAUNCHERACTIVITY, 3000);
}
}
activity_launcher.xml布局文件,很简单,要记住这里的LinearLayout使用的背景是layout_list_start_pic,跟主题theme使用一样的背景,这样就消除了背景不一样的效果。这里要自己试试才知道这样做的好处和效果。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/layout_list_start_pic" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="@string/hello_world" />
</LinearLayout>
AndroidManifest.xml,这里注意application使用的theme是AppTheme,而LauncherActivity使用的主题是StartAppTheme。这样做的效果是只要LauncherActivity使用StartAppTheme主题,其他Activity都是用AppTheme主题哦。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.launcheractivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LauncherActivity"
android:label="@string/app_name"
android:theme="@style/StartAppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
styles.xml,2个主题设置
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/white</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="StartAppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/layout_list_start_pic</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
layout_list_start_pic.xml 启动页面使用这个作为背景图片
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置整个屏幕背景为白色 -->
<item >
<color android:color="@color/white"/>
</item>
<!-- 中间logo -->
<item >
<bitmap
android:gravity="center"
android:src="@drawable/ic_launcher" />
</item>
<!-- 底部图表 -->
<item android:bottom="10dp">
<bitmap
android:gravity="bottom|center_horizontal"
android:src="@drawable/copyright" />
</item>
</layer-list>
- Android横竖屏切换及其对应布局加载问题
第一,横竖屏切换连带横竖屏布局问题: 如果要让软件在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局. 可以通过以下两种方法来切换布局: 1)在res目录下建立layout-land ...
- Android 全屏显示
Android全屏显示: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst ...
- Android横竖屏切换小结
Android横竖屏切换小结 (老样子,图片啥的详细文档,可以下载后观看 http://files.cnblogs.com/franksunny/635350788930000000.pdf) And ...
- 【转】Android横竖屏切换问题
Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...
- Android横竖屏切换及其相应布局载入问题
第一.横竖屏切换连带载入多屏布局问题: 假设要让软件在横竖屏之间切换.因为横竖屏的高宽会发生转换,有可能会要求不同的布局. 能够通过下面两种方法来切换布局: 1)在res文件夹下建立layout-la ...
- Android横竖屏切换处理
Android横竖屏要解决的问题应该就两个: 1.布局问题:2.重新载入问题 一.布局问题: 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你 ...
- Android横竖屏切换总结
Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...
- android MIPI屏 导航栏丢失
/**************************************************************************** * android MIPI屏 导航栏丢失 ...
- 【转】android 电容屏(三):驱动调试之驱动程序分析篇
关键词:android 电容屏 tp 工作队列 中断 坐点计算 电容屏主要参数平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV310( ...
随机推荐
- thinkphp5.0 中简单处理微信支付异步通知
public function wx_notify(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; libxml_disable_ent ...
- mui或者uni退出app
在安卓上可以使用 //1.1 var backButtonPress = 0; $.back = function(event) { backButtonPress++; if(backButtonP ...
- 总结敏捷开发之Scrum
敏捷开发的概念 敏捷开发是一种以人为核心,迭代,循序渐进的开发方法. 为什么说是以人为核心?传统的瀑布模型是以文档驱动的,但是在敏捷中,只写少量的文档,注重的是人与人之间面对面的交流. 什么是迭代?迭 ...
- spring boot 启动原理详细解析
我们开发任何一个Spring Boot项目,都会用到如下的启动类 1 @SpringBootApplication 2 public class Application { 3 public stat ...
- .net core + xunit 集成测试
xunit地址:https://github.com/xunit/xunit 一.利用请求来测试接口,主要是测试webapi控制器方法 ①添加xunit项目 ,然后引用我们的主项目,nuget: Mi ...
- 面试题(Python)
面试题 字符串反向输出 s = "给阿姨倒杯卡布奇诺"反向输出S:print(s[::-1]) 面试必问:赋值,浅拷贝,深拷贝 赋值:多个变量指到相同内存浅拷贝中所有的元素,不管第 ...
- sqlserver读取日志以及复制
首选,在事务日志中,到底有多少是需要复制的?使用以下命令,可以确定事务日志中被标志为复制的命令有多少. USE test GO SELECT count(*) FROM ::fn_dblog(NULL ...
- 在 Docker 中运行 SpringBoot 应用
创建 SpringBoot 项目 用 Idea 创建一个 SpringBoot 项目,编写一个接口: package cloud.dockerdemo import org.springframewo ...
- dotnet core 之 CORS使用示例
这里列举几个经过验证的可用的CORS使用示例, 方便在需要的时候可以直接使用 示例1 #region snippet2 public void ConfigureServices(IServiceCo ...
- 在Unity 5中优化SkinnedMeshRenderer
过早优化是万恶之源”——Donald Knuth 不少开发者在前期开发过程中对算法等类似的开销都甚少关心,而是更倾向于尽可能简单的解决某个问题,后面必要时再进行优化.这能极大加速开发进度 ...