在运行时更改android操作栏的背景色

在运行时更改android操作栏的背景色,android,android-actionbar-compat,Android,Android Actionbar Compat,根据VibrantColor图像,我试图在运行时更改android操作栏的背景色 我找到了一些推荐信,但都不适合我 我有这个代码来从图像中获取颜色,它可以工作 public int getImageColor(Bitmap foto) { Palette palette = Palette.generate(foto); List<Palette.Swatch> swatchList = palette.getSwatches(); int maior

根据VibrantColor图像,我试图在运行时更改android操作栏的背景色

我找到了一些推荐信,但都不适合我

我有这个代码来从图像中获取颜色,它可以工作

public int getImageColor(Bitmap foto) {
    Palette palette = Palette.generate(foto);

    List<Palette.Swatch> swatchList = palette.getSwatches();

    int maiorPopulação = 0;

    int color = 0;

    for (Palette.Swatch sw : swatchList){
        if (sw.getPopulation() > maiorPopulação) {
            maiorPopulação = sw.getPopulation();
            color = sw.getRgb();
        }
    }

    return palette.getVibrantColor(color);
}
我在片段上从
onCreate
和从
onAttach
调用了
alteraCor
,但两者都不起作用

编辑:

我使用以下风格:

    <style name="VendasFacil" parent="@style/Theme.AppCompat">
        <item name="android:textColor">@color/Fonte</item>
        <item name="android:background">@color/fundoPrimeiroPlano</item>
        <item name="android:windowBackground">@color/fundoPrimeiroPlano</item>
        <item name="actionBarTheme">@style/VendasFacil.ActionBar</item>
        <item name="android:actionBarTheme" tools:ignore="NewApi">@style/VendasFacil.ActionBar</item>
    </style>

    <style name="VendasFacil.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:background">@color/fundoEscuro</item>
        <item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
        <item name="displayOptions">showHome|homeAsUp|showTitle</item>
        <item name="android:homeAsUpIndicator">@drawable/ic_launcher</item>
        <item name="homeAsUpIndicator">@drawable/ic_drawer</item>
    </style>

@颜色/字体
@彩色/眼底镜
@彩色/眼底镜
@style/VendasFacil.ActionBar
@style/VendasFacil.ActionBar
@颜色/眼底
showHome | homeAsUp | showTitle
showHome | homeAsUp | showTitle
@牵引式/集成电路发射器
@可抽出式/集成电路抽屉

我没有以前要用作背景以将其放入样式文件中的颜色。这就好像颜色是由用户在运行时定义的。

您需要为此创建一种样式。然后,您可以在运行时更改样式,使actionbar反映它

这可能有用:

以下是更改actionbar颜色的样式示例:

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

任何十六进制颜色代码

您可以使用。它可以生成XML文件。复制到您的项目中。这很简单也很好

嘿,您的代码中似乎有一个小错误:

actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(
        Color.red(color),
        Color.green(color),
        Color.blue(color)
    )));
您正在将r、g和b值设置为颜色的十六进制值。您需要做的只是:

actionBar.setBackgroundDrawable(new ColorDrawable(color));

我没有以前用于将其放入样式文件的颜色。这就好像颜色是由用户在运行时定义的。我没有以前想用作背景的颜色,无法将其放入样式文件中。这就好像颜色是由用户在运行时定义的一样。新的AppCompat不推荐使用操作栏样式生成器
actionBar.setBackgroundDrawable(new ColorDrawable(color));