我无法改变ActionBar颜色

时间:2017-08-28 12:13:08

标签: java android android-actionbar

我想使用我的自定义主题来更改ActionBar颜色,但我不能 这是我的styles.xml文件:

<resources>

<!-- Base application theme. -->
<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>
</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" />

<style name="NewTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/CustomActionBar</item>
</style>

<style name="CustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/pink</item>
</style>

这里是我的AndroidManifest.xml文件:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/NewTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

看看这个:

android:theme="@style/AppTheme.NoActionBar"

如果我将此主题更改为CustomActionBar或NewTheme,一切看起来都很正常,但是当我启动虚拟设备时,我看到了:

enter image description here

我研究了很多资源,但我找不到答案 我怎样才能解决这个问题?请帮帮我!

3 个答案:

答案 0 :(得分:0)

将主题更改回android:theme="@style/AppTheme.NoActionBar"

确保您的布局xml正确无误。以下是正确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"
                                                 >

    <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="BACKGROUND COLOR"
            app:titleTextColor="TITLE TEXT COLOR"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <!--CONTENT-->

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

现在,您可以使用android:backgroundapp:titleTextColor等属性设置工具栏样式,也可以使用AppTheme.PopupOverlay设置操作栏的样式。

答案 1 :(得分:0)

这样做。定义自定义样式。

<resources>
<style name="myActionTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">YourColor</item>
</style>
</resources>

答案 2 :(得分:0)

您可以通过编程方式执行此操作

ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
  Drawable drawable = getResources().getDrawable(R.drawable.yourColor);
  actionBar.setBackgroundDrawable(drawable);
}
相关问题