【问题标题】:Set the StatusBar color through theme.?通过主题设置状态栏颜色。?
【发布时间】:2016-11-11 18:25:15
【问题描述】:

我打算通过styles.xml改变状态栏颜色但是不能设置状态栏颜色。我不想通过编码设置状态栏的颜色,我只想通过theme/style

Activity.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"
    android:fitsSystemWindows="true"
    tools:context="eazitouk.eazitoclient.mainclasses.TestActivity">

    <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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <include layout="@layout/content_test" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

Manifest.xml

<application
        android:name="eazitouk.eazitoclient.miscs.AppController"
        android:icon="@drawable/ic_launchicon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".TestActivity"
            android:label="@string/title_activity_test"
            android:theme="@style/AppTheme1">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="ColorPrimary">#df4a43</color>
    <color name="ColorPrimaryDark">#c8423c</color>

    <color name="ColorPrimary1">#FFCF3B</color>
    <color name="ColorPrimaryLight">#F3A2A0</color>
    <color name="ColorPrimaryLightShade">#e56e66</color>

</resources>

Style.xml

<resources>

    <!-- Application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
    </style>

    <style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <item name="android:textColorSecondary">@color/accentcolor</item>-->
    </style>
</resources>

这是我的 style.xml,AppTheme 适用于应用程序,AppTheme1 适用于我的活动,但无法设置状态栏颜色

【问题讨论】:

  • 你在colors.xml中设置颜色了吗?
  • 是的,我设置了。但不能工作。
  • @akhil batlawala 你在测试哪个设备??
  • 其中的版本。比如LolliPopMarshMallow。或喜欢APi level 21Api level 20
  • Android 6.0,API 23 @Ironman

标签: android android-layout colors styles statusbar


【解决方案1】:

我已经测试了您的代码。现在更改为您的style 有问题。所以按照我的方式获取输出。

Android manifest.xml.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">

        <activity
            android:name=".TestActivity"
            android:label="@string/title_activity_test"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="primaryColor">#EF6C00</color>
    <color name="primaryColorDark">#E65100</color>
    <color name="accentcolor">#0072BA</color>

</resources>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="colorAccent">@color/accentcolor</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

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

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

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


</resources>

styles.xml(v21)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

</resources>

最终输出:

还有更多事情要记住,你已经设置了targetSdkVersion 23

【讨论】:

  • @akhil batlawala 嘿 CGPIT 你解决了吗??
  • 应用程序将不幸停止。
  • java.lang.RuntimeException:无法启动活动 ComponentInfo{eazitouk.eazitoclient/eazitouk.eazitoclient.mainclasses.Dashboard}:java.lang.IllegalStateException:此活动已经有窗口提供的操作栏装饰风格。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
【解决方案2】:

尝试在六进制代码中赋予颜色。 color.xml 文件中的颜色ColorPrimaryDark 似乎没有包含您想要的颜色。

试试:

<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="colorPrimaryDark">#fad</item>
    ...
</style>

#fad的地方使用你的颜色代码

【讨论】:

  • 您必须在 style.xml 文件中直接将颜色代码提供给项目标签。
【解决方案3】:

ColorPrimaryDark 是状态栏颜色对应的颜色。

https://developer.android.com/training/material/theme.html

所以:

<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>

将您想要的颜色添加到 colors.xml 中,名称为 ColorPrimaryDark

【讨论】:

  • 我知道但不能工作。请给我另一个解决方案。
  • colors.xml 中没有你的颜色,或者你的 AppTheme 或 AppTheme1 都没有被选中。
  • 请编辑您的问题并添加colors.xml。另外,“AppTheme 适用于应用程序,AppTheme1 适用于我的活动但无法设置状态栏颜色”是什么意思?
  • 请检查我的问题
  • AppTheme apply to application 表示适用于整个应用程序,AppTheme1 to my activity 表示适用于单个活动。
【解决方案4】:

要更改状态栏颜色,您需要 api 级别 19 且大于 21。 更多信息请查看此链接。 http://androidmysolution.blogspot.in/2015/12/change-status-bar-color-for-your.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    相关资源
    最近更新 更多