Android 如何更改不确定的 ProgressBar 的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2729180/
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 07:07:00  来源:igfitidea点击:

How to change the color of an indefinite ProgressBar?

androidlayoutprogress-bar

提问by Janusz

I have a progressbar with the following style: style="?android:attr/android:progressBarStyleSmall"

我有一个具有以下样式的进度条: style="?android:attr/android:progressBarStyleSmall"

Sadly the bar is nearly white and in my case displayed on a white background. The progressbar is nearly invisible because of that.

可悲的是,酒吧几乎是白色的,在我的情况下显示在白色背景上。因此,进度条几乎不可见。

How can I change the color of the progressbar? A darker grey would be great.

如何更改进度条的颜色?深灰色会很棒。

采纳答案by Prashast

The reason why the bar is of that color is because of the style you have selected. That is a fixed style and uses the system themes for the generation of UI elements.

条形是那种颜色的原因是因为您选择的样式。这是一种固定样式,使用系统主题生成 UI 元素。

Look at the source code from here. This progressBarStyleSmall attribute uses the styles defined here.

这里查看源代码。此progressBarStyleSmall 属性使用此处定义的样式。

<style name="Widget.ProgressBar.Small">
    <item name="android:indeterminateDrawable">@android:drawable/progress_small_white</item>
    <item name="android:minWidth">16dip</item>
    <item name="android:maxWidth">16dip</item>
    <item name="android:minHeight">16dip</item>
    <item name="android:maxHeight">16dip</item>
</style>

Just create a custom style following the example from the android source code. You would need to replace the android:indeterminateDrawable to what you want.

只需按照 android 源代码中的示例创建自定义样式。您需要将 android:indeterminateDrawable 替换为您想要的。

回答by Vishal Pandey

progressBar.getIndeterminateDrawable().setColorFilter(
            getResources().getColor(R.color.light_light_purple),
            android.graphics.PorterDuff.Mode.SRC_IN);

This code changes the default holo inderminate drawable color to your own color. Define your color code and replace R.color.light_light_purpleto R.color.your_color_code.

此代码将默认的 Holo inderminate 可绘制颜色更改为您自己的颜色。定义您的颜色代码并替换R.color.light_light_purpleR.color.your_color_code.

回答by Anm

To get a black ProgressBar, use one of the inverse styles:

要获得黑色 ProgressBar,请使用其中一种反向样式:

<ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Large.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Small.Inverse"/>

回答by Casper-Nodes

public static void setColorOfProgressBar(ProgressBar mProgressBar, int mColor){
    mProgressBar.getIndeterminateDrawable().setColorFilter(mColor, android.graphics.PorterDuff.Mode.MULTIPLY);
}

回答by Janusz

And the answer was already on SO:

答案已经在 SO 上了:

stackoverflow.com/questions/2020882/how-to-change-progress-bars-progress-color-in-android

stackoverflow.com/questions/2020882/how-to-change-progress-bars-progress-color-in-android

You can simply change the whole drawable that is shown in the progressbar via the android:indeterminateDrawableattribute.

您可以通过android:indeterminateDrawable属性简单地更改进度条中显示的整个可绘制对象。

回答by Macarse

I had a very hard time trying to change it's color. To solve it, I got android's src code and did my own ProgressBar class.

我很难尝试改变它的颜色。为了解决这个问题,我得到了 android 的 src 代码并做了我自己的 ProgressBar 类。

Not the best answer, but it worked for me.

不是最好的答案,但它对我有用。

回答by ubzack

Just a note, to use

只是一个注释,使用

<ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/> 

or any of the inverse progress bars, you have to set your minimum android sdk requirement to level 4 (Android 1.6).

或任何反向进度条,您必须将最低 android sdk 要求设置为 4 级(Android 1.6)。