显示状态栏并改变颜色

show statusbar and change color

我正在尝试让我的 activity 显示我的状态栏。

without statusbar

我的activity的风格是这样的:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="Theme.AppCompat">
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
    <item name="android:actionMenuTextColor">@color/ifix_gray_dark</item>
    <item name="android:typeface">serif</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="colorControlActivated">@color/white</item>

    <item name="actionBarStyle">@style/ActionBarTheme</item>
    <item name="actionMenuTextColor">@color/ifix_gray_dark</item>
</style>

<style name="ActivityBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="actionBarSize">35dp</item>
</style>

<style name="ActionBarTheme" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/actionbar_background</item>
    <item name="android:backgroundStacked">@drawable/actionbar_background</item>
    <item name="android:backgroundSplit">@drawable/actionbar_background</item>

    <item name="background">@drawable/actionbar_background</item>
    <item name="backgroundStacked">@drawable/actionbar_background</item>
    <item name="backgroundSplit">@drawable/actionbar_background</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="buttonTint">@color/rosa_iguana</item>
</style>
</resources>

我有 v21 样式:

 <style name="ActivityBar" parent="Theme.AppTheme.NoActionBar">
    <item name="colorPrimaryDark">@color/black</item>
    <item name="android:statusBarColor">@color/black</item>
</style>

我找到这段代码来显示我的状态栏,但我把它涂成了白色

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI(hasFocus);
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private void hideSystemUI(boolean hasFocus) {
    if (hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }
}

example status bar white here

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.projextx">
    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <!-- for card.io card scanning -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- Added for MobileAppTracking -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />
    <application
        android:name=".Application"
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@drawable/launcher_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activities.SplashActivity"
            android:screenOrientation="portrait"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ActivitySelectCountry"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.HomeActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ActivityBar"
            android:windowSoftInputMode="adjustPan"/>
        <activity
            android:name=".activities.ActivitySegment"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".activities.ActivityProductDetail"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".activities.ActivityPagerPagoOption"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan" />
    </application>
</manifest>

我真的很想在样式中使用一些解决方案,因为我必须将它应用到另外 2 个活动中。

感谢您的帮助。

这是对我来说完全没问题的代码

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="actionBarSize">35dp</item>
</style>

您可以尝试一下,或者您还应该检查以下内容:

  • if color Black代码为真黑(#000000)
  • 在清单的 activity 声明中,您已正确设置主题

如果仍然无法正常工作,请 post 布局 activity 并显示

我通过在 values\styles 和 v21\styles 中包含相同的基础解决了这个问题,就像这样。

  <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

由于您 v21\style 中使用的主题不同,请将其更改为与 values\styles 中相同的基本主题。希望这可能对您有所帮助,因为我已经按照上述相同的方式解决了这个问题。

最后不知道为什么状态栏不显示,这段代码解决了我的问题:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI(hasFocus);
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private void hideSystemUI(boolean hasFocus) {
    if (hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getColor(R.color.black));
        }
    }
}

感谢您的帮助。