【发布时间】:2015-10-08 02:49:01
【问题描述】:
如何更改我在 Android Studio 中制作的应用的状态栏颜色,以便它在 Android Lollipop 5.0 或更高版本上更改为静态颜色并且不会在运行较低版本 Android 操作系统的移动设备上崩溃。
【问题讨论】:
-
嘿!我还发布了详细的答案..随时查看!
标签: android android-5.0-lollipop android-statusbar
如何更改我在 Android Studio 中制作的应用的状态栏颜色,以便它在 Android Lollipop 5.0 或更高版本上更改为静态颜色并且不会在运行较低版本 Android 操作系统的移动设备上崩溃。
【问题讨论】:
标签: android android-5.0-lollipop android-statusbar
试试这个,这对我有用
//changing statusbar
if (android.os.Build.VERSION.SDK_INT >= 21){
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.primary_dark));
}
用于改变颜色的 ActionBar :
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#ffff00</item>
</style>
并在应用程序标签中的清单上声明
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyActionBar" >
希望对你有帮助
【讨论】:
<color name="primary_dark">#3f41ac</color> 您可以通过更改该 hexa 来更改颜色匹配操作栏
您可以通过 2 种方式做到这一点,或者为您的主题添加属性,例如
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">>#ED3B3B</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#BE2F2F</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#4DB6AC</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>
并且你的 AndroidManifest 的应用标签必须有一个带有 AppTheme 的主题属性
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"> // This
或者,如果您希望在稍后阶段动态更改状态栏颜色,例如单击按钮或完成加载图像后,您可以在 Java 类中使用
if (android.os.Build.VERSION.SDK_INT >= 21){
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor("your color"));
}
【讨论】:
Toolbar。您可以用工具栏替换操作栏,它可以让您更好地控制您的操作栏。chris.banes.me/2014/10/17/appcompat-v21
请将此添加到您的应用样式中
<item name="colorPrimary">>#FFFF00</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#000000</item>
【讨论】: