【问题标题】:How to change the StatusBar color of the splash screen?如何更改启动画面的状态栏颜色?
【发布时间】:2019-03-17 17:31:57
【问题描述】:

我正在使用 React Native 来构建我的 android 应用程序,并且我已经按照 tutorial 设置了我的启动画面。这是result。我的主要问题是状态栏颜色变为黑色,我无法通过在我的styles.xml 文件中添加<item name="colorPrimaryDark">@color/colorPrimaryDark</item> 和在我的colors.xml 文件中添加<color name="blue">#009CD7</color><color name="colorPrimaryDark">#009CD7</color> 来解决这个问题。

额外问题:如何在不硬编码边距的情况下使图像居中,以便无论应用程序在什么设备上运行,它都保持在中心?

【问题讨论】:

  • 将此行添加到您的样式中:@color/your_color
  • @AslamHossin 不幸的是,没有任何改变
  • 你的主题是 MaterialTheme 那么它会起作用吗?
  • 你已经把上面这行放在了 v21/style.xml 状态栏颜色改变了 API 级别 21 之后引入的。

标签: android react-native splash-screen


【解决方案1】:

在您当前的启动主题中再添加一个colorPrimaryDark 项目或创建一个新项目。

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@android:color/white</item>
    <item name="android:windowLightStatusBar">true</item>
    <item name="android:background">@drawable/background_splash</item>
    <item name="colorPrimaryDark">@android:color/white</item>
</style>

然后在显示 SplashScreen 时将其用作第二个参数。

public class MainActivity extends ReactActivity {

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

【讨论】:

    【解决方案2】:

    在 android/app/src/main/res/values/styles.xml 中为此创建样式定义:

    <?xml version="1.0" encoding="utf-8"?>
        <resources>
             <style name="SplashTheme" parent="SplashScreen_SplashTheme">
                 <item name="colorPrimaryDark">your_color</item>
             </style>
       </resources>
    

    现在您已更改 show 方法以包含您的自定义样式:

    SplashScreen.show(this, R.style.SplashTheme);
    

    你现在没了:)

    【讨论】:

      【解决方案3】:

      如果您看到类似的错误 -> "int can not be boolean" 只需将第三个参数添加到 MainActivity.java

      SplashScreen.show(this, R.style.SplashTheme, true);
      

      【讨论】:

        【解决方案4】:

        如果您想更改任务栏颜色,只需在输出函数中添加您想要的活动和颜色即可。

        fun setTaskBarColored(colored: Int,activity: Activity) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    activity.window!!.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                    activity.window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                    activity.window!!.statusBarColor = ContextCompat.getColor(activity, colored)
                }
            }
        

        同样适用于导航栏

            fun setNavigationBarColored(colored: Int, activity: Activity){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    activity.window!!.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                    activity.window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                    activity.window!!.navigationBarColor =  ContextCompat.getColor(activity, colored)
                }
            }
        

        【讨论】:

        • 我应该在哪里添加这些“活动”?谢谢
        【解决方案5】:

        您可以简单地将此 JSX 代码添加到应用程序的根目录,而无需编辑任何本机文件。

        <StatusBar backgroundColor="blue" barStyle="light-content" />
        

        此外,为了使您的图像居中,用视图包裹它并添加样式“{justifyContent: 'center', alignItems: 'center'}”。如果这不起作用,则将此样式直接添加到您的图像组件“alignSelf:'center'”

        【讨论】:

        • 在初始屏幕中,我得到了灰色,只有当我的导航准备好显示它时,它才会变为我选择的颜色
        猜你喜欢
        • 2017-11-05
        • 2021-06-05
        • 2015-11-27
        • 2021-11-05
        • 1970-01-01
        • 2015-05-09
        • 1970-01-01
        相关资源
        最近更新 更多