如何在Android中更改进度条的进度颜色
我在 Android 应用程序中使用水平进度条,并且我想更改其进度颜色(默认情况下为黄色)。我如何使用code
(而不是XML)来做到这一点?
I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code
(not XML)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
这不是以编程方式,但我认为无论如何它可以帮助很多人。
我尝试了很多,最有效的方法是将这一行添加到 .xml 文件中的 ProgressBar 中:
所以最后这段代码为我完成了:
此解决方案适用于 API 21+
This is not programmatically but I think it could help a lot of people anyway.
I tried a lot and the most efficient way was to add this lines to my ProgressBar in the .xml File:
So in the end this code did it for me:
This solution works for API 21+
对于水平进度条,您也可以使用
ColorFilter
,如下所示:注意: 这会修改您的所有进度条的外观应用程序。要仅修改一个特定的进度条,请执行以下操作:
如果 ProgressBar 不确定,则使用
getIndependentDrawable()
而不是getProgressDrawable()
。从 Lollipop (API 21) 开始,您可以设置进度色调:
For a horizontal ProgressBar, you can use a
ColorFilter
, too, like this:Note: This modifies the appearance of all progress bars in your app. To only modify one specific progress bar, do this:
If progressBar is indeterminate then use
getIndeterminateDrawable()
instead ofgetProgressDrawable()
.Since Lollipop (API 21) you can set a progress tint:
很抱歉,这不是答案,但是是什么推动了从代码中设置它的需求?
如果定义正确,
.setProgressDrawable
应该可以工作I'm sorry that it's not the answer, but what's driving the requirement setting it from code ?
And
.setProgressDrawable
should work if it's defined correctly对于我不确定的进度条(旋转器),我只是在可绘制对象上设置了一个滤色器。效果很好,而且只有一行。
将颜色设置为红色的示例:
For my indeterminate progressbar (spinner) I just set a color filter on the drawable. Works great and just one line.
Example where setting color to red:
这是一个老问题了,不过这里没有提到使用主题。如果您的默认主题使用
AppCompat
,则ProgressBar
的颜色将为您定义的colorAccent
。更改
colorAccent
也会更改ProgressBar
的颜色,但这些更改也会反映在多个位置。因此,如果您想要为特定的PregressBar
使用不同的颜色,您可以通过将主题应用于该ProgressBar
来实现:扩展默认主题并覆盖
颜色强调
并在
ProgressBar
中添加android:theme
属性:< /p>所以它看起来像这样:
所以你只是改变一个
colorAccent
对于您的特定ProgressBar
。注意:使用
style
将不起作用。您只需要使用android:theme
。您可以在此处找到主题的更多用法:https://plus.google.com/u/0/ +AndroidDevelopers/posts/JXHKyhsWHAH
This is an old question, but using theme is not mentioned here. If your default theme is using
AppCompat
, yourProgressBar
's color will becolorAccent
you have defined.Changing
colorAccent
will also change yourProgressBar
's color, but there changes also reflects at multiple places. So, if you want a different color just for a specificPregressBar
you can do that by applying theme to thatProgressBar
:Extend your default theme and override
colorAccent
And in
ProgressBar
add theandroid:theme
attribute:So it will look something like this:
So you are just changing a
colorAccent
for your particularProgressBar
.Note: Using
style
will not work. You need to useandroid:theme
only.You can find more use of theme here: https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH
所有 API
中创建主题
如果使用所有 API,只需在样式style.xml
并在进行中使用
API 级别 21 及更高版本
如果在 API 级别 21 及更高版本中使用,只需使用此代码:
All API
if use all API just create the theme in style
style.xml
and use in progress
API level 21 and higher
if used in API level 21 and higher just use this code:
这对我有用。它也适用于较低版本。将其添加到您的 syles.xml
并在 xml 中像这样使用它
This works for me. It also works for lower version too. Add this to your syles.xml
And use it like this in xml
这对我有用:
This worked for me :
根据一些建议,您可以指定形状和可剪辑的颜色,然后设置它。我以编程方式进行此工作。我就是这样做的..
首先确保导入了可绘制库..
import android.graphics.drawable.*;
然后使用类似于下面的代码;
as per some of the suggestions, you CAN specify a shape and clipdrawable with a colour, then set it. I have this working programatically. This is how I do it..
First make sure you import the drawable library..
import android.graphics.drawable.*;
Then use the code similar to below;
如果不确定:
if Indeterminate:
相信我,最简单的解决方案就是将其粘贴到 ProgressBar 中:
Trust me, the easiest solution is just paste this inside progressBar :
如今,在 2016 年,我发现一些 Lollipop 之前的设备不支持
colorAccent
设置,因此我对所有 API 的最终解决方案如下:为了奖励积分,它不使用任何已弃用的代码。试试吧!
Nowadays in 2016 I found some pre-Lollipop devices don't honour the
colorAccent
setting, so my final solution for all APIs is now the following:For bonus points, it doesn't use any deprecated code. Try it!
对于我来说,XML Works 中的SDK 版本 21 及更高版本
非常简单。
For SDK ver 21 and above
in XML Works for me, is easy enough.
这就是我所做的。工作了。
进度条:
progressdrawable.xml:
这里使用渐变来改变你喜欢的颜色。 android:toDegrees="X" 增加 X 的值,进度条快速旋转。减少,旋转缓慢。根据您的需求定制。
示例:
THis is what i did. Worked.
ProgressBar:
progressdrawable.xml:
Here use gradient to change colour as you like. And android:toDegrees="X" increse the value of X and progressbar rotate fast. Decrease and it rotate slow.Customize according to your needs.
sample:
Xml 内添加 ProgressBar
在 SDK 版本 21 及更高版本的
Add in ProgressBar inside of Xml
For SDK ver 21 and above
在修改默认进度栏的外观/感觉时遇到了同样的问题。以下是一些希望对人们有所帮助的更多信息:)
a-z0-9_.
(即没有大写!)R.drawable.filename
myProgressBar.setProgressDrawable(...)
,但是您不能仅将自定义布局引用为R.drawable.filename
,您需要将其作为Drawable
检索:Hit the same problem while working on modifying the look/feel of the default progress bar. Here is some more info that will hopefully help people :)
a-z0-9_.
(ie. no capitals!)R.drawable.filename
myProgressBar.setProgressDrawable(...)
, however you need can't just refer to your custom layout asR.drawable.filename
, you need to retrieve it as aDrawable
:您可以尝试更改您的样式、主题,或在任何您想要的地方使用 android:independentTint="@color/yourColor" ,但只有一种方法可以在任何 Android SKD 版本上使用:
如果您的进度条不是不确定的,请使用:
如果你的进度条不确定,请使用:
遗憾的是 Android 是如此混乱!
You can try to change your Styles, Themes, or using android:indeterminateTint="@color/yourColor" anywhere you want, but there's just one way o doing that will work on any Android SKD version:
If you progress bar is not indeterminate, please use:
If you progress bar is indeterminate, please use:
It's sad that Android is such a mess!
对于默认(不确定),
添加
确定
For default ( indeterminate )
add
for determinate
我是如何在水平进度条中做到这一点的:
How I did it in horizontal ProgressBar:
此答案中可能没有提到一件事:
如果您的主题继承自
Theme.AppCompat
,ProgressBar
将采用您定义为的颜色主题中的“colorAccent”
。所以,使用..
..将自动将 ProgressBar 的颜色着色为
@color/custom_color
。There's probably one thing that hasn't been referred to in this answer:
If your theme is inheriting from
Theme.AppCompat
,ProgressBar
will assume the color you defined as"colorAccent"
in your theme.So, using..
..will tint the color of the ProgressBar automagically to the
@color/custom_color
.最简单的解决方案如果您想更改布局 xml 文件中的颜色,请使用以下代码并使用 inminatedTint 属性作为您想要的颜色。
Simplest Solution if you want to change the colour in the layout xml file, use the below code and use indeterminateTint property for your desired color.
更改进度条的前景色和背景色的最简单方法就是
添加
The most simple way of changing the foreground and background colour of a progress bar is
just add
这个解决方案对我有用:
This solution worked for me :
更改水平进度条颜色(在 kotlin 中):
更改不确定的进度条颜色:
最后它通常会为棒棒糖进度条着色
To change horizontal ProgressBar color (in kotlin):
To change indeterminate ProgressBar color:
And it finally normally tint pre-lollipop progressBars
只需使用:
Simply use:
另一件小事是,如果您继承基本主题,主题解决方案确实有效,因此对于应用程序紧凑型,您的主题应该是:
然后在进度栏主题中设置它
One more little thing, the theme solution does work if you inherit a base theme, so for app compact your theme should be:
And then set this in the progress bar theme
只需使用:
simply use:
水平进度条自定义材质样式:
更改水平进度条的背景颜色和进度。
通过设置样式属性将其应用到进度条,对于自定义材质样式和自定义进度条,请检查 http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples
Horizontal progress bar custom material style :
To change color of background and progress of horizontal progress bar.
Apply it to progress bar by setting style attribute, for custom material styles and custom progress bar check http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples
使用 android.support.v4.graphics.drawable.DrawableCompat :
Use the android.support.v4.graphics.drawable.DrawableCompat: