[英]How to change the StatusBar color of the splash screen?
我正在使用 React Native 來構建我的 android 應用程序,並且我已經按照本教程來設置我的啟動畫面。 這是結果。 我的主要問題是狀態欄顏色變為黑色,我無法通過在我的styles.xml
文件中使用<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
和<color name="blue">#009CD7</color>
, <color name="colorPrimaryDark">#009CD7</color>
在我的colors.xml
文件中。
額外的問題:如何在不硬編碼邊距的情況下使圖像居中,以便無論應用程序在什么設備上運行,它都保持在中心?
在您當前的啟動主題中再添加一個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, true);
super.onCreate(savedInstanceState);
}
}
如果您看到類似的錯誤 -> “int can not be boolean” 只需將第三個參數添加到 MainActivity.java
SplashScreen.show(this, R.style.SplashTheme, true);
在 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>
現在您可以更改顯示方法以包含您的自定義樣式:
SplashScreen.show(this, R.style.SplashTheme);
你現在不行了:)
如果您想更改任務欄顏色,只需在輸出函數中添加您想要的活動和顏色。
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)
}
}
同樣適用於 NavigationBar
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)
}
}
您可以簡單地將此 JSX 代碼添加到應用程序的根目錄,而無需編輯任何本機文件。
<StatusBar backgroundColor="blue" barStyle="light-content" />
此外,為了使您的圖像居中,用視圖包裹它並添加樣式“{justifyContent: 'center', alignItems: 'center'}”。 如果這不起作用,則將此樣式直接添加到您的圖像組件“alignSelf:'center'”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.