【问题标题】:Status bar always appears black with AppCompatActivityAppCompatActivity 状态栏始终显示为黑色
【发布时间】:2016-05-05 19:37:10
【问题描述】:

我的状态栏总是显示为黑色。我尝试了多次修复,但没有任何技巧......我不敢相信这么简单的事情就这么复杂

这是我的 Style.xml:

<style name="AppTheme" parent="BaseTheme" />

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/cobalt</item>
    <item name="colorPrimaryDark">@color/cobalt_dark</item>
    <item name="colorControlHighlight">@color/cobalt_light</item>
    
    <item name="colorAccent">@color/cobalt_light</item>
</style>

这是我的 v21/style.xml:

<style name="AppTheme" parent="BaseTheme">
    <item name="android:colorControlHighlight">@color/cobalt_light</item>
    
    <item name="android:statusBarColor">@color/cobalt_light</item>
</style>

这是我的活动:

public class ActivityLogin extends AppCompatActivity {
 //...
}

我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.plante.android.cobalt"
    android:versionName="7">

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="21" />

    <application
        android:name=".CobaltApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme”>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:windowSoftInputMode="adjustResize"
            android:name=".activityv2.ActivityLogin"
            android:label="@string/app_name"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".activityv2.ActivityHome"
            android:label="Home"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tag_filter" />
        </activity>
        <activity
            android:name=".activityv2.ActivitySplash"
            android:configChanges="orientation|keyboardHidden|screenSize"
            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>
    </application>

【问题讨论】:

  • 你能复制/粘贴你的清单文件,尤其是 标签内的内容吗?

标签: android android-appcompat statusbar


【解决方案1】:

您应该将您的样式“BaseTheme”重命名为“AppTheme.Base”,以便它重载基本应用程序主题。

或在清单文件的标签中添加参数:

android:theme="@style/BaseTheme"

【讨论】:

  • 我更新了帖子,我已经在base style.xml中添加了
【解决方案2】:

如果解决方案被证明是虚幻的,您总是可以通过编程方式设置颜色。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.WHITE);
}

【讨论】:

    【解决方案3】:

    好的,问题来了:

    defaultConfig {
        applicationId "com.plante.android.cobalt"
        minSdkVersion 17
        targetSdkVersion 18
        versionCode 7
        multiDexEnabled false
        versionName "7"
    }
    

    我应该把 api 21 及以上版本让它工作......

    【讨论】:

      【解决方案4】:

      尝试使用此属性更新您的 v21/样式:

      <item name="android:statusBarColor">@color/color_primary</item>
      

      更新: 在与作者讨论后,我们决定使用 targetSdkVersion=18。我们可以从 \sdk\extras\android\support\v7\appcompat\res\values\attrs.xml

      中注意到
      <!-- Dark variant of the primary branding color. By default, this is the
      color applied to the status bar (via statusBarColor) and navigation bar (via 
      navigationBarColor). -->
      <attr name="colorPrimaryDark" format="color" />
      

      由于 statusBarColor 属性 在 API 级别 21 中添加,因此没有记录在棒棒糖前更改状态栏颜色的方法。

      【讨论】:

      • 它说“无法解析符号'@color/color_primary'”......因为我没有那个名字的颜色......
      • 哦,抱歉,这只是示例。你应该用你自己的颜色替换它。关键思想是 v21+ API 的 android:statusBarColor 属性
      • 好的,但我已经有这个属性,如我的帖子中所述...:/
      • 我错过了。请尝试 true
      • 我在您的帖子中没有看到 android:windowDrawsSystemBarBackgrounds。
      猜你喜欢
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多