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

时间:2022-11-21 14:50:54

I'm trying to change the background color of android action bar at runtime, according to an image VibrantColor.

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

I found some references, but neither worked for me.

我找到了一些参考文献,但都没有对我有用。

Change action bar color in android

更改android中的操作栏颜色

How do I change the background color of the ActionBar of an ActionBarActivity using XML?

如何使用XML更改ActionBarActivity的ActionBar的背景颜色?

I have this code to get the color from the image, and it works.

我有这个代码来从图像中获取颜色,它的工作原理。

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);
}

Then I try to set the returned color to the ActionBar:

然后我尝试将返回的颜色设置为ActionBar:

public static void alteraCor(Fragment fragmento, int color) {
    ActionBarActivity atividade = (ActionBarActivity)fragmento.getActivity();

    android.support.v7.app.ActionBar actionBar = atividade.getSupportActionBar();

    if ( fragmento instanceof Detalhes )
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(
                Color.red(color),
                Color.green(color),
                Color.blue(color)
            )));
        else
            actionBar.setBackgroundDrawable(new ColorDrawable(fragmento.getResources().getColor(R.color.fundoEscuro)));
    }

I called alteraCor from onCreate and from onAttach on Fragment, but neither of them worked.

我从onCreate和片段上的onAttach调用alteraCor,但它们都没有工作。

EDIT:

I use the folowing Style:

我使用下面的风格:

    <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>

I do not have the color I want to use as background previously to put it in a style file. It is as if the color was defined by the user at runtime.

我之前没有想要用作背景的颜色将它放在样式文件中。就像用户在运行时定义颜色一样。

3 个解决方案

#1


0  

You will need to create a style for that. You can then change the style at runtime to have the actionbar reflect it.

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

This might be useful: http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles

这可能很有用:http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles

Here is an example of a style changing the actionbar color:

以下是更改操作栏颜色的样式示例:

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

#2


0  

You can use Android Action Bar Style Generator . It's generate XML files. Copy to your project. It's easy and nice.

您可以使用Android Action Bar Style Generator。它生成XML文件。复制到您的项目。这很容易也很好。

#3


0  

Hey there appears to be a small bug in your code:

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

actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(
        Color.red(color),
        Color.green(color),
        Color.blue(color)
    )));

Your are setting the r the g and the b value to the hex value of the color. What you need to do instead is simply:

您正在将r和g值设置为颜色的十六进制值。你需要做的只是:

actionBar.setBackgroundDrawable(new ColorDrawable(color));

#1


0  

You will need to create a style for that. You can then change the style at runtime to have the actionbar reflect it.

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

This might be useful: http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles

这可能很有用:http://developer.android.com/guide/topics/ui/actionbar.html#AdvancedStyles

Here is an example of a style changing the actionbar color:

以下是更改操作栏颜色的样式示例:

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

#2


0  

You can use Android Action Bar Style Generator . It's generate XML files. Copy to your project. It's easy and nice.

您可以使用Android Action Bar Style Generator。它生成XML文件。复制到您的项目。这很容易也很好。

#3


0  

Hey there appears to be a small bug in your code:

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

actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(
        Color.red(color),
        Color.green(color),
        Color.blue(color)
    )));

Your are setting the r the g and the b value to the hex value of the color. What you need to do instead is simply:

您正在将r和g值设置为颜色的十六进制值。你需要做的只是:

actionBar.setBackgroundDrawable(new ColorDrawable(color));