【问题标题】:Activity's status bar color is different for no apparent reasonActivity 的状态栏颜色无缘无故地不同
【发布时间】:2017-08-07 02:03:37
【问题描述】:

我创建了一个新活动,由于某种原因,当它被使用时,它在状态栏中使用了不同的颜色。奇怪的是,在“设计”预览中它呈现了正确的颜色。

下面是两张截图,颜色越深越好。

不正确 正确

主题与其他 .xml 布局相同,所以我很难真正弄清楚这个错误的来源。下面是布局的代码:

activity_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.brewguide.android.coffeebrewguide.MenuActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            />

    </android.support.design.widget.AppBarLayout>

<LinearLayout
    android:id="@+id/myfragment"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

我不确定包含哪些其他代码可用于诊断此问题。


编辑:

styles.xml

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

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="DescriptionTextView">
    <item name="android:textColor">@color/colorPrimaryDark</item>
    <item name="android:background">@color/mainBackground</item>
</style>

<!--style for the start button in the clock view layout-->
<style name="AppTheme.Button.Start" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/complimentaryLeft</item>
    <item name="android:textColor">@color/cardview_dark_background</item>
</style>

<!--style for the stop button in the clock view layout-->
<style name="AppTheme.Button.Stop" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/colorPrimary</item>
    <item name="android:textColor">@color/cardview_dark_background</item>
</style>

<!--style for the reset button in the clock view layout-->
<style name="AppTheme.Button.Reset" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/colorAccent</item>
    <item name="android:textColor">@color/cardview_light_background</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

【问题讨论】:

  • 分享您的styles.xml。
  • @azizbekian 刚刚添加到问题中
  • 也分享AndroidManifest.xml。

标签: android xml statusbar


【解决方案1】:

我有同样的问题。对我来说,这是第二个 styles.xml 文件的问题(仍然不确定为什么我有两个)。导致问题的特定行是:

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

我在您发布的 styles.xml 中没有看到这一点,但也许它会对其他人有所帮助。

【讨论】:

  • 这解决了我的问题。我的 style.xml(v21) 文件中有那行
【解决方案2】:

检查colors.xml 中名为primary_dark 的颜色并将其更改为与primary 相同,因为Android 使用该(primary_dark)颜色为状态栏着色。

【讨论】:

  • 将colorPrimaryDark 更改为primary. 没有做任何事情:/
  • @JohnSnow 请张贴您的colors.xml
【解决方案3】:

设法使用以下行以编程方式设置颜色:

getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));

不过,我希望在 .xml 中完成此操作。

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多