Java 创建或更改状态栏颜色的正确方法是什么?
我有一个安静的大问题,改变我的状态栏的颜色。 我正在使用的设备告诉我android 5.1,但是像Java 创建或更改状态栏颜色的正确方法是什么?,java,android,notifications,statusbar,Java,Android,Notifications,Statusbar,我有一个安静的大问题,改变我的状态栏的颜色。 我正在使用的设备告诉我android 5.1,但是像windowTranslucentStatus或android:colorPrimaryDark或android:statusBarColor这样的工作人员根本不工作(半透明的透明度为20%左右),其他任何东西都不工作 所以它看起来像是安卓5.0下的东西 所以我尝试了一些应用程序——其中一个确实有效,因为它们在旧的应用程序前面创建了自己的状态栏。但第一件事我不会使用不同的应用程序,第二件事这个应用程
windowTranslucentStatus
或android:colorPrimaryDark
或android:statusBarColor
这样的工作人员根本不工作(半透明的透明度为20%左右),其他任何东西都不工作
所以它看起来像是安卓5.0下的东西
所以我尝试了一些应用程序——其中一个确实有效,因为它们在旧的应用程序前面创建了自己的状态栏。但第一件事我不会使用不同的应用程序,第二件事这个应用程序不能做动画通知,我在我的项目中使用
所以我在状态栏前面用我的颜色做了一个按钮,但现在我需要在那里添加所有图标。(我只需要wifi、蓝牙、电池和时间)就是这样,但我发现这很难做到
所以现在我有点被卡住了,有人知道如何改变颜色或者如何复制通知图标吗?我不需要滑动通知栏,我只需要看到它。您可以在活动或应用程序主题的
androidMenifest.xml
中设置它,例如
<activity
android:theme="@style/activityTheme"
android:name=".MyActivity"
android:screenOrientation="portrait" />
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));
}
清单
<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">
hmmm它可以更改。。。mabie becouse Theme.AppCompat为深色,Theme.AppCompat.Light.darkaActionBar为浅色
<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]
@颜色/颜色\u红色\u真实
@颜色/颜色\u红色\u真实
@颜色/颜色\u红色\u真实
[![在此处输入图像描述][1][1]
@颜色/颜色\u红色\u真实
@颜色/颜色\u红色\u真实
@颜色/颜色\u红色\u真实
所以最后一件事是它能工作这是唯一一个代码,它不会留下状态栏阴影。。。不知道为什么,但是的,这是我的解决方案。。。我在我的背景红色的样式。。。在所有的屏幕(布局)上,我添加了从边缘到边缘的所有布局。。。所以我的背景是这个布局。。。真正的背景只用于状态栏
像半透明状态和我的颜色的小布局这样的解决方案确实在状态栏后面留下了阴影,所以我永远无法得到我所需要的确切颜色。。。而像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);
}
在your styles.xml中,您需要设置背景颜色(此颜色将是您的状态栏颜色):
@颜色/您的\u状态\u栏\u颜色
在所有布局中,您需要添加布局,该布局将成为您的背景:
<LinearLayout
android:background="@color/YOUR_BACKGROUND_COLOR"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
/>
当然,如果您想使用@color/只是名称。。。您需要在colors.xml中设置:
<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>
#cf031c
#383838
谢谢,我来自捷克,不太懂英语@androidanthontanks@saeed foroughipublicvoid onCreate(最终捆绑包保存在stancestate){if(Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.LOLLIPOP){deleniuctactivity.Window.statusBarColor=ContextCompat.getColor(this,R.color.color\u red\u real)}因此,我尝试了这个方法,但无法从样式中解析符号窗口。它始终有效。您可以给出一些您正在样式和清单文件中尝试的代码trygetWindow()
上面的代码是用kotlin语言编写的,让我把它编辑成javamabie等等…我正在测试编程方式我很抱歉@Muhammad Muzammil Sharif我不知道SDK版本是什么意思在我的电脑上有检查安卓10和安卓8我现在知道为什么了…半透明确实起作用…通过这种方式它不会产生阴影…是的解决方案!
<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>
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);
}
<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_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
/>
<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>