在 android 启动画面中为状态栏着色

color the statusbar in android splash screen

我在我的 android 应用程序中添加了启动画面,我想在显示时为状态栏添加颜色。

这是drawable下的文件内容:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/colorPrimary" />

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_home"/>
    </item>

</layer-list>

我添加了这种样式:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splashscreen</item>
</style>
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}

colorPrimaryDark 项目添加到您的主题应该可以解决问题:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splashscreen</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

插入themes.xml & temes.xml(夜):

<item name="colorPrimaryVariant">@color/black</item>

在 styles.xml 中的当前初始主题中添加 colorPrimaryDark 项:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimaryDark">@color/statusbar_bg</item>
</style>

如果您为状态栏选择浅色,您可以添加:

<item name="android:windowLightStatusBar">true</item>

在colors.xml里面的资源添加:

<color name="statusbar_bg">#FFFFFF</color>

然后在 MainActivity.java 中显示 SplashScreen 时将其用作第二个参数:

public class MainActivity extends ReactActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this, R.style.SplashTheme);
        super.onCreate(savedInstanceState);
    }
}