美文网首页
06. 正确写splash页面(学习笔记)

06. 正确写splash页面(学习笔记)

作者: Jsonzhang | 来源:发表于2016-12-26 18:09 被阅读49次

参考资料:http://blog.csdn.net/zivensonice/article/details/51691136

参考资料:http://blog.csdn.net/yanzhenjie1003/article/details/52201896

去掉状态栏的代码:

getWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

我们平时这样写

<activity
       android:name=".SplashActivity"
       android:screenOrientation="portrait"
       android:theme="@style/ThemeSplash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>
<style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="windowActionBar">false</item>
         //一定要,否则有状态栏显示,不能全屏
        <item name="windowNoTitle">true</item> 
</style>

然后是SplashAcitivity的布局文件,用一张图片作为背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg_splash"
    tools:context="com.example.makeapp.SplashActivity">
</LinearLayout>

然后是SplashActivity的代码,延迟2秒跳转到主页面

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //延迟2S跳转
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 2000);
    }
}

但是此时你会发现一个问题,就是点击进入app后,会先白屏或是黑屏后,然后才会进入splash页面。

正确的姿势

我们只需要对style中的ThemeSplash进行改动,只是给windowbackground设置背景就ok:

<style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/你的splash图片</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="windowActionBar">false</item>
         //一定要,否则有状态栏显示,不能全屏
        <item name="windowNoTitle">true</item> 
</style>

或者也可以这样:

<style name="LuncherTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">    
        <item name="android:windowBackground">@drawable/splash</item>
</style>

ok,这样就会是app秒起,而不会闪屏了

相关文章

网友评论

      本文标题:06. 正确写splash页面(学习笔记)

      本文链接:https://www.haomeiwen.com/subject/zgcjvttx.html