如何更改进度条的默认颜色?
我在我的 Activty 中使用圆形 ProgressBar
。我的问题是它在我的页面上无法正确显示,因为我的页面的 BG 颜色与 ProgressBar 相同。那么我如何更改ProgressBar 使其正确可见? 请帮忙
I am using a circular ProgressBar
in my Activty.My Problem is this it is not visible properly on my page because my page's BG color is same as ProgressBar .So how can I change the color of ProgressBar to make it properly Visible?
Please Help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
请创建一个名为progress.xml的xml文件并将其放在res/xml文件夹中,并在该xml文件中写入以下代码。
创建此 xml 文件后,将进度条背景设置为此 xml ..
就像
Please make one xml file name progress.xml and put it in res/xml folder and write the below code in that xml file.
after creating this xml file set your progressbars background as this xml ..
Like
最简单的方法是使用
android:inminatedTint 属性:
The easiest way is to use
android:indeterminateTint attribute:
这是一个老问题了,不过这里没有提到使用主题。如果您的默认主题使用
AppCompat
,则ProgressBar
的颜色将为您定义的colorAccent
。更改
colorAccent
也会更改ProgressBar
的颜色,但这些更改也会反映在多个位置。因此,如果您想要为特定的ProgressBar
使用不同的颜色,您可以通过单独将主题应用于该ProgressBar
来实现:扩展默认主题并覆盖
颜色强调
并在
ProgressBar
中添加android:theme
属性:< /p>所以它看起来像这样:
所以你只是改变一个
colorAccent
对于您的特定ProgressBar
。注意:使用
style
将不起作用。您只需要使用android:theme
。您可以在此处找到主题的更多用法:https://plus.google.com /u/0/+AndroidDevelopers/posts/JXHKyhsWHAH
编辑
以下是通过编程方式更改 ProgressBar 颜色的代码。
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 these changes also reflects at multiple places. So, if you want a different color just for a specificProgressBar
you can do that by applying theme to thatProgressBar
alone: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
Edit
Here is the code for changing the color of ProgressBar by programatically.
转到 .xml 文件中的进度条声明:
并粘贴这行代码:
上面的示例允许您将进度条的颜色更改为 <强>白色。 现在您可以将其调整为您想要的颜色。
仅适用于 Android API >= 21
Go to the declaration of the progress bar in your .xml file :
And paste this line of code :
The example above allows you to change the color of your progress bar to white. Now it's up to you to adapt it to the color you want.
Only for Android API >= 21
在 xml 中你可以像这样应用颜色
in xml u can apply color like this
对我来说,这两行必须在那里才能工作并更改颜色:
PS:但它仅适用于 android 21
for me, these two lines had to be there for it to work and change the color:
PS: but its only available from android 21
创建进度可绘制文件
并设置
android:progressDrawable="@drawable/app_progressbar_back"
像这样
create progress drawable file
and set
android:progressDrawable="@drawable/app_progressbar_back"
like this
您还可以在活动 oncreate 或片段 onViewCreated() 中以编程方式更改进度条的颜色,
这里应用的颜色是“0xFFFF0000”,即红色。您可以根据您的需要进行更改
You can also change the color of your progressbar programmatically in your activities oncreate or your fragments onViewCreated()
Here the color applied is "0xFFFF0000" which is red. You can change it according to your needs
这是通过 java 代码实现的方法。
here is how you can do it by java code.
这是一些解释。要更改
Inminated ProgressBar
的背景,您需要一个旋转环,因为它会旋转。在@Chirag的回答中,他定义了一个旋转环并将其应用于ProgressBar的背景。如果您有一个水平进度条,那么您需要一个矩形。PS:我没有尝试过其他形状。
Here is some explanation. For changing background of a
Indeterminate ProgressBar
, you need a rotating ring because it rotates. In the answer of @Chirag, he has defined a rotating ring and applied it to the background ofProgressBar
. If you have aHorizontal ProgressBar
then you need arectangle
.PS: I have not tried with other shapes.
我使用的是安卓2.3.1版本。我创建了一个名为progressbar的xml文件,并在布局进度条标签中调用它 android:progressDrawable="@drawable/progressbar"
I'm using android 2.3.1 version. I create a xml file called progressbar and call it in the layout progress bar tag android:progressDrawable="@drawable/progressbar"
奇拉格的答案是正确的,但并非全部。
我认为 XML 文件应该如下所示:
The Chirag's answer is right, but not all.
I think the XML file should look like this:
使用
android:inminatedDrawable="@drawable/.."
肯定会改变进度条背景的颜色。但就我而言,进展是确定的。我可以使用进度栏中的以下行更改进度和进度背景的颜色:
Using
android:indeterminateDrawable="@drawable/.."
definitely changes the color of the progress bar background. But in my case, the progress was determinate.I was able to change the color of both progress and progress background using the below lines within the progress bar:
对于任何使用 CircularProgressIndicator 的人,您可以尝试:
For anyone using CircularProgressIndicator, you can try:
您也可以仅更改属性
并保留此样式
you also can change only the attribute
and keep this style
尝试在您的 XML 中使用它
Try using this in your XML