创建或更改状态栏颜色的正确方法是啥?
Posted
技术标签:
【中文标题】创建或更改状态栏颜色的正确方法是啥?【英文标题】:Whats the right approach to create or change color of a status bar?创建或更改状态栏颜色的正确方法是什么? 【发布时间】:2020-05-28 00:30:34 【问题描述】:我在更改状态栏颜色时遇到了一个大问题。
我正在使用的设备告诉我 android 5.1,但像 windowTranslucentStatus
或 android:colorPrimaryDark
或 android:statusBarColor
这样的工作人员根本不起作用(半透明有 20% 的暗透明度),其他任何东西都不起作用。
所以它似乎是 android 5.0 下的东西。
所以我尝试了一些应用程序 - 一个确实有效,因为它们在旧应用程序前面制作了自己的状态栏。但第一件事我不会使用不同的应用程序,第二件事是这个应用程序不能做我在项目中使用的动画通知
所以我在状态栏前用我的颜色制作了一个按钮,但现在我需要在那里添加所有图标。 (我只需要 wifi、蓝牙、电池、时间。)就是这样,但我发现这很难。
所以现在我有点卡住了有没有人知道如何更改该颜色或如何以某种方式复制通知图标?我不需要滑动通知栏我只需要看到它。
【问题讨论】:
感谢我来自捷克,不太懂英语@AndroidAnton 感谢@saeed foroughi 【参考方案1】:您可以在活动或应用主题中将其设置为androidMenifest.xml
,例如
<activity
android:theme="@style/activityTheme"
android:name=".MyActivity"
android:screenOrientation="portrait" />
在style.xml
<style name="activityTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
colorPrimaryDark
是活动的状态栏颜色
如果你想以编程方式更改它,你可以使用下面的代码
用于活动
Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
window.statusBarColor = ContextCompat.getColor(this, R.color.YOUR_COLOR)
Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.COLOR));
对于片段
Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
activity.window.statusBarColor = ContextCompat.getColor(this, R.color.YOUR_COLOR)
Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.COLOR));
【讨论】:
public void onCreate(final Bundle savedInstanceState) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) DeleniUctuActivity.Window.statusBarColor = ContextCompat.getColor(this, R.color.color_red_real );所以我尝试了这个,但它无法解析符号窗口 从样式中它总是有效的,您能否提供一些您在样式和清单文件中尝试的代码 试试getWindow()
以上代码是 kotlin 语言让我把它编辑成 java
mabie 等等...我正在测试编程方式【参考方案2】:
清单
<application android:name="com.smartpos.pocket.app.PocketApplication" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:allowBackup="true" android:theme="@style/AppTheme">
<activity android:name="com.smartpos.pocket.activity.impl.StartUpActivity" android:theme="@style/AppTheme">
样式(我在 api 17 有项目)
<style name="AppTheme" parent="AppCompat">
</style>
样式 v21
<style name="AppCompat" parent="Theme.AppCompat">
<item name="colorAccent">@color/color_red_real</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="colorPrimary">@color/color_red_real</item>
<item name="colorPrimaryDark">@color/color_red_real</item>
</style>
状态栏不会改变任何我认为设备是层并且没有 android 5.1 或者我不知道...
【讨论】:
对不起@Muhammad Muzammil Sharif 我不知道你在我的电脑上的SDK版本是什么,检查Android 10和Android 8【参考方案3】:嗯,它可以改变...mabie,因为 Theme.AppCompat 是深色的,而 Theme.AppCompat.Light.DarkActionBar 是浅色的
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/color_red_real</item>
<item name="colorPrimaryDark">@color/color_red_real</item>
<item name="colorAccent">@color/color_red_real</item>
</style>[![enter image description here][1]][1]
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/color_red_real</item>
<item name="colorPrimaryDark">@color/color_red_real</item>
<item name="colorAccent">@color/color_red_real</item>
</style>
【讨论】:
我现在知道为什么了...半透明确实有效...通过这种方式,它不会产生阴影...哦,是的【参考方案4】:所以最后一件事是的,它确实有效,这是唯一一个不会留下状态栏阴影的代码...不知道为什么,但是是的,这对我来说是解决方案...我将背景设置为红色...在所有屏幕(布局)上,我从边到边添加了所有布局...所以我的背景现在是这个布局...而真实背景仅用于状态栏...
像 TranslucentStatus 这样的解决方案和我的颜色的小布局确实在状态栏后面留下了阴影,所以我永远无法得到我需要的颜色......而像 statusbarcolor 或 primarydarkcolor 这样的解决方案以编程方式或样式从来没有工作......所以如果你有这个问题你可以做这个丑陋的解决方案......但它绝对有效......
非常感谢@Muhammad Muzammil Sharif,没有他,我永远不会达到这一点......(我公司的 3 个人试图整周都这样做)
这是最终版本...现在我想隐藏一些系统通知...但我做不到这可能...再次感谢大家
所以在你所有的 java 活动中你都需要这个:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
您需要在您的styles.xml 中设置背景颜色(此颜色将是您的状态栏颜色):
<style name="AppCompat" parent="Theme.AppCompat">
<item name="android:colorBackground">@color/YOUR_STATUS_BAR_COLOR</item>
</style>
在所有布局中,您都需要添加作为背景的布局:
<LinearLayout
android:background="@color/YOUR_BACKGROUND_COLOR"
android:orientation="vertical"
android:layout_
android:layout_
android:gravity="bottom"
/>
当然,如果您想使用 @color/just name... 您需要在 colors.xml 中进行设置:
<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>
【讨论】:
这是解决方案!以上是关于创建或更改状态栏颜色的正确方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章