在 Android 中通过 CODE ONLY 更改进度条颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10951978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:32:44  来源:igfitidea点击:

Change progressbar color through CODE ONLY in Android

androidcolorsprogress-bar

提问by hico

I have a progressBar using the ProgressBar class.

我有一个使用 ProgressBar 类的 progressBar。

Just doing this:

只是这样做:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

I need to change the color of that one, using input value like so:

我需要更改那个颜色,使用如下输入值:

int color = "red in RGB value".progressBar.setColor(color)

or something like that...

或类似的东西...

I can't use an XML layoutbecause the progress bar is customizable for users.

我不能使用XML 布局,因为进度条是为用户自定义的。

采纳答案by hico

As I found help on a topic here but can't remember the link, I'm posting my full solution which works great for my needs:

当我在这里找到有关某个主题的帮助但不记得链接时,我发布了我的完整解决方案,它非常适合我的需求:

    // Draw a simple progressBar from xml
    progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

    // Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)
    String color = colorDecToHex(75, 150, 160);

    // Define a shape with rounded corners
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners,     null, null));

    // Sets the progressBar color
    pgDrawable.getPaint().setColor(Color.parseColor(color));

    // Adds the drawable to your progressBar
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    progressBar.setProgressDrawable(progress);

    // Sets a background to have the 3D effect
    progressBar.setBackgroundDrawable(Utils.getActivity().getResources()
            .getDrawable(android.R.drawable.progress_horizontal));

    // Adds your progressBar to your layout
    contentLayout.addView(progressBar);

And here is the code to convert DECIMAL color values to HEXADECIMAL:

这是将 DECIMAL 颜色值转换为 HEXADECIMAL 的代码:

public static String colorDecToHex(int p_red, int p_green, int p_blue)
{
    String red = Integer.toHexString(p_red);
    String green = Integer.toHexString(p_green);
    String blue = Integer.toHexString(p_blue);

    if (red.length() == 1)
    {
        red = "0" + red;
    }
    if (green.length() == 1)
    {
        green = "0" + green;
    }
    if (blue.length() == 1)
    {
        blue = "0" + blue;
    }

    String colorHex = "#" + red + green + blue;
    return colorHex;
}

I think the last method is not that clean but it works well.

我认为最后一种方法不是那么干净,但效果很好。

Hope this well help, too much time wasted on this progressbar.

希望这有帮助,在这个进度条上浪费了太多时间。

回答by Mit Bhatt

This will help much no need to do so much coding :)

这将有很大帮助,无需进行太多编码:)

ProgressBar spinner = new android.widget.ProgressBar(
            context,
            null,
            android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);

回答by brbsBruno

In the case that you need to tint the background and the progress bar in different colors.

如果您需要为背景和进度条着色不同的颜色。

progress_drawable.xml

progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape android:shape="rectangle" >
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/green" />
            </shape>
        </clip>
    </item>
</layer-list>

Its possible, programmatically, to decompound its layer-list items, and tint them separately:

它可以以编程方式分解其图层列表项,并分别为它们着色:

LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);
Drawable progressDrawable = progressBarDrawable.getDrawable(1);

backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN);
progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);

回答by Jose M Vidal

Update

更新

In newer versions of Android (21 works), you can change the color of a progressbar programmatically by just using setProgressTintList.

在较新版本的 Android(21 个作品)中,您只需使用setProgressTintList.

To set it red, use:

要将其设置为红色,请使用:

//bar is a ProgressBar
bar.setProgressTintList(ColorStateList.valueOf(Color.RED));

回答by Moritz

It is possible to colorize the Progress bar by setting the color filter on the progress bar drawable:

可以通过在可绘制的进度条上设置颜色过滤器来为进度条着色:

Drawable drawable = progressBar.getProgressDrawable();
drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt));

回答by Pir Fahim Shah

if you want to change color of progress bar programmatically then you copy past this code it is working 100%

如果你想以编程方式改变进度条的颜色,那么你复制过去这段代码,它工作 100%

 mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);   

回答by Lubos Ilcik

This works for me with AppCompat:

这适用于我的 AppCompat:

DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);

回答by Daniel Park

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

It only works above API 21

它仅适用于 API 21 以上

回答by Hiren Patel

I have given default colorin xmlby drawable.

我已经通过drawablexml 中给出了默认颜色

I have changed it programatically.

我已经以编程方式对其进行了更改。

activity_splasg.xml:

activity_splasg.xml:

<ProgressBar
       android:id="@+id/splashProgressBar"
       android:progressDrawable="@drawable/splash_progress_drawable"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="100"
       android:progress="50"
       style="?android:attr/progressBarStyleHorizontal"
       android:layout_alignParentBottom="true" />

splash_progress_drawable.xml:

splash_progress_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid
                android:color="@android:color/transparent" />
        </shape>
    </item>

    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <solid
                    android:color="#e5c771" />
            </shape>
        </clip>
    </item>

</layer-list>

Now How to change ProgressDrawable color programatically.

现在如何以编程方式更改ProgressDrawable 颜色

ProgressBar splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);

Drawable bgDrawable = splashProgressBar.getProgressDrawable();
bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
splashProgressBar.setProgressDrawable(bgDrawable);

Hope this will help you.

希望这会帮助你。

回答by Cruceo

This post is what you're looking for: How to change progress bar's progress color in Android

这篇文章正是您要找的:如何在 Android 中更改进度条的进度颜色

If you want the user to choose their own colors, just make multiple-drawable XML files for each color, and select them based on the user's choice.

如果希望用户自己选择颜色,只需为每种颜色制作多个可绘制的 XML 文件,并根据用户的选择进行选择。