【问题标题】:Change background color of android action bar at runtime在运行时更改android操作栏的背景颜色
【发布时间】:2015-04-17 01:32:21
【问题描述】:

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

我找到了一些参考资料,但都不适合我。

Change action bar color in android

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

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

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

然后我尝试将返回的颜色设置为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)));
    }

我在 Fragment 上从 onCreateonAttach 拨打了 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>

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

【问题讨论】:

    标签: android android-actionbar-compat


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我没有以前想要使用的颜色来将其放入样式文件中。就好像颜色是用户在运行时定义的一样。
    【解决方案2】:

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

    【讨论】:

    • 我之前没有想要用作背景的颜色来将其放入样式文件中。就好像颜色是用户在运行时定义的一样。
    • 操作栏样式生成器已被新的 AppCompat 弃用
    【解决方案3】:

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

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

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

    actionBar.setBackgroundDrawable(new ColorDrawable(color));
    

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多