创建或更改状态栏颜色的正确方法是什么?

问题描述 投票:1回答:4

我在更改状态栏的颜色时遇到一个很大的安静问题。我正在使用的设备告诉我android 5.1,但像windowTranslucentStatusandroid:colorPrimaryDarkandroid:statusBarColor这样的人员根本不起作用(半透明的像有20%的黑暗透明度),其他任何都不起作用。

所以看起来好像是在Android 5.0下。

因此,我尝试了一些应用程序-之所以起作用,是因为它们在旧的应用程序之前创建了自己的状态栏。但第一件事是我不会使用其他应用程序,第二件事是该应用程序无法执行我在项目中使用的动画通知

所以我在状态栏的前面做了一个颜色为我的按钮,但是现在我需要在其中添加所有图标。 (我只需要wifi,蓝牙,电池,时间。)就是这样,但是我发现很难。

所以现在我有点卡住,没有人知道hack如何更改该颜色或如何以某种方式复制通知图标吗?我不需要滑动通知栏,只需要看到它即可。

java android notifications statusbar
4个回答
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));
}

0
投票

清单

<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>

styles 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或我不知道...

enter image description here


0
投票

hmmm,可以更改... 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]

enter image description here

<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>

enter image description here


0
投票

所以最后一件是的,它确实起作用,这只是一个代码错误,不会在状态栏阴影后面留下……不知道为什么,但是是的,这对我来说是解决方案。在所有屏幕(布局)上,我从边缘到边缘在所有布局后面添加...所以我的背景现在是此布局...而实际背景仅用于状态栏...

像TranslucentStatus这样的解决方案和我的颜色的小布局确实在状态栏后面留下了阴影,所以我永远无法获得我需要的那种颜色...而像statusbarcolor或primarydarkcolor这样的解决方案无法通过编程方式或样式无法使用...所以如果您有这个问题你可以做这个丑陋的解决方案...但它肯定可以工作...

非常感谢@Muhammad Muzammil Sharif,没有他,我将永远无法做到这一点……(我公司的3个人试图整周都这样做)

这是最后的决定...现在我想隐藏一些提示系统通知...但是我认为不可能......再次感谢大家enter image description here

enter image description here

enter image description here

因此,在您所有的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_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    />

当然,如果您想使用@ color / just name ...,则需要在colors.xml中进行设置:

<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>
© www.soinside.com 2019 - 2024. All rights reserved.