支持ActionBar不会使用API​​ 21显示正确的颜色

Arn*_*old 10 android android-theme

我正在尝试制作一个appcompat主题,但颜色不起作用,图标也不显示......也许我错过了一些东西.这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <color name="background_test">#410000</color>
    <color name="font_general">#ffffff</color>
    <style name="MyTheme"
        parent="@style/Theme.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>

    </style>
    <style name="AudioFileInfoOverlayText">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingBottom">2dp</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:textSize">18sp</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">1</item>
    </style>

    <!-- general styles for the action bar -->
    <style name="MyActionBar"
        parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:icon">@drawable/ic_launcher</item>
        <item name="icon">@drawable/ic_launcher</item>
        <item name="background">@color/background_test</item>
        <item name="android:background">@color/background_test</item>
  </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

我应该添加什么才能使它工作?

编辑:

它也不适用于工具栏......我设置后:

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)

它将失去所有用户定义的主题,只会获得父主题...这是我的xmls(是的,我有很多冗余/测试值,因为我想尝试一切)

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        background="?attr/colorPrimary"
        app:popupTheme="@style/MyActionBarStyle"
        android:background="?attr/colorPrimary"
        app:theme="@style/MyActionBarStyle"
        />

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- the theme applied to the application or activity -->
    <color name="background_test">#410000</color>
    <color name="font_general">#ffffff</color>
    <!-- general styles for the action bar -->
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="actionBarStyle">@style/MyActionBarStyle</item>
        <item name="android:panelColorBackground">@color/background_test</item>
        <item name="android:headerBackground">@color/background_test</item>
        <item name="android:colorBackground">@color/background_test</item>
        <item name="android:backgroundSplit">@color/background_test</item>

        <item name="backgroundSplit">@color/background_test</item>
        <item name="colorAccent">@color/background_test</item>

        <!-- Here we setting appcompat’s actionBarStyle -->


        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/background_test</item>
        <item name="colorPrimaryDark">@color/background_test</item>
    </style>

    <style name="MyActionBarStyle">
        <item name="colorAccent">@color/background_test</item>

        <!-- Here we setting appcompat’s actionBarStyle -->
        <item name="android:panelColorBackground">@color/background_test</item>
        <item name="android:headerBackground">@color/background_test</item>
        <item name="android:colorBackground">@color/background_test</item>
        <item name="android:backgroundSplit">@color/background_test</item>

        <item name="backgroundSplit">@color/background_test</item>

        <!-- ...and here we setting appcompat’s color theming attrs -->
        <item name="colorPrimary">@color/background_test</item>
        <item name="colorPrimaryDark">@color/background_test</item>
        <item name="android:background">@color/background_test</item>

        <item name="background">@color/background_test</item>
    </style>

    <style name="AudioFileInfoOverlayText">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingBottom">2dp</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:textSize">18sp</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">1</item>
    </style>



</resources>
Run Code Online (Sandbox Code Playgroud)

截图:

http://i.imgur.com/4iohVwP.png
Run Code Online (Sandbox Code Playgroud)

新编辑:它无法使用代码......

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
    ActionBar test=getSupportActionBar();

    test.setLogo(R.drawable.ic_launcher); //this is the only one working...
    test.setBackgroundDrawable(new ColorDrawable(0x410000));

    test.setDisplayShowTitleEnabled(false);
    test.setDisplayShowTitleEnabled(true);
Run Code Online (Sandbox Code Playgroud)

编辑:

报告了新的Appcompat-v7的一些错误,这可能是一个错误:

http://code.google.com/p/android/issues/detail?id=77763&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
Run Code Online (Sandbox Code Playgroud)

Chr*_*nes 7

如果你正在使用Toolbar那么你actionBarStyle不能使用.

您需要直接设置工具栏样式:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="..."
    android:background="..."
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:titleTextAppearance="..."
    app:subtitleTextAppearance="..." />
Run Code Online (Sandbox Code Playgroud)

您可以自定义更多属性,这些只是示例.

我不确定为什么getSupportActionBar().setBackgroundDrawable(...)不适合你,我只是在不同的地方试过它,它工作正常.


Gab*_*tti 1

这是一个例子:

\n\n
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">  \n    <!-- colorPrimary is used for the default action bar background -->\n    <item name="colorPrimary">@color/my_color</item>\n\n    <!-- colorPrimaryDark is used for the status bar -->\n    <item name="colorPrimaryDark">@color/my_darker_color</item>\n\n    <!-- colorAccent is used as the default value for colorControlActivated\n         which is used to tint widgets -->\n    <item name="colorAccent">@color/accent</item>\n\n    <!-- appcompat\xe2\x80\x99s actionBarStyle -->\n   <item name="actionBarStyle">@style/MyActionBarStyle</item>\n\n</style>  \n
Run Code Online (Sandbox Code Playgroud)\n\n

另外我建议您使用新的Toolbar

\n\n

您可以在这里找到更多信息:

\n\n

https://chris.banes.me/2014/10/17/appcompat-v21/

\n