在 Android 启动界面中更改状态栏颜色

16

我在我的安卓应用中添加了一个启动画面,并希望在显示时为状态栏设置颜色。

这是位于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>
4个回答

25

在您的主题中添加colorPrimaryDark项即可解决问题:

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

如果你有两个styles.xml,其中另一个res\values\styles.xml是为旧版Android设计的,那么你需要提到这应该在res\values-v21\styles.xml中完成。 - Ayman Salah
3
在旧设备上,colorPrimaryDark 不会生效,你可以在 res\values\styles.xml 中设置它。对于简单的主题,比如启动界面主题,我更喜欢保持简单。 - Jérémy Reynaud
2
您还可以添加 <item name="colorPrimary">@color/colorPrimary</item>,这样如果用户最小化应用程序,则会显示您首选的应用程序标题颜色。 - Levon Petrosyan

1

将内容插入到 themes.xmltemes.xml(night) 中:

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

0
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));
}

0
在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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接