显示状态栏并更改颜色

时间:2017-07-07 18:52:15

标签: android statusbar

我想让我的活动显示我的状态栏。

without statusbar

我的活动风格是这样的:

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

对于Style 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>

我真的希望在样式中有一些解决方案,因为我必须将它应用于另外两个活动。

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

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

@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));
        }
    }
}

感谢您的帮助。

答案 1 :(得分:0)

以下代码对我来说完全没问题

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

您可以尝试,也可以检查以下内容:

  • 如果颜色黑色代码真的是黑色(#000000)
  • 在清单活动声明中,您已正确设置主题

如果仍然无法正常工作,请发布活动和清单的布局

答案 2 :(得分:0)

我通过将它们包含在值\ styles和v21 \ styles中来解决这个问题。

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

由于v21 \ style中使用的主题不同,因此请将其更改为与values \ styles中相同的基本主题。希望这可以帮到你,因为我已经按照我上面描述的方式解决了这个问题。