如何更改进度条的默认颜色?

发布于 2024-11-16 15:35:01 字数 148 浏览 3 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

何处潇湘 2024-11-23 15:35:01

请创建一个名为progress.xml的xml文件并将其放在res/xml文件夹中,并在该xml文件中写入以下代码。

 <?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
    android:toDegrees="360">
    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">

        <size android:width="76dip" android:height="76dip" />
        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#447a29" 
            android:endColor="#447a29"
            android:angle="0"
             />
    </shape>
</rotate> 

创建此 xml 文件后,将进度条背景设置为此 xml ..

就像

<ProgressBar
  android:id="@+id/ProgressBar01" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background = "@xml/progress">

Please make one xml file name progress.xml and put it in res/xml folder and write the below code in that xml file.

 <?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
    android:toDegrees="360">
    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">

        <size android:width="76dip" android:height="76dip" />
        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#447a29" 
            android:endColor="#447a29"
            android:angle="0"
             />
    </shape>
</rotate> 

after creating this xml file set your progressbars background as this xml ..

Like

<ProgressBar
  android:id="@+id/ProgressBar01" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background = "@xml/progress">
挥剑断情 2024-11-23 15:35:01

最简单的方法是使用
android:inminatedTint 属性:

<ProgressBar
        android:indeterminate="true"
        android:indeterminateTint="@android:color/black"
        ........... />

The easiest way is to use
android:indeterminateTint attribute:

<ProgressBar
        android:indeterminate="true"
        android:indeterminateTint="@android:color/black"
        ........... />
2024-11-23 15:35:01

这是一个老问题了,不过这里没有提到使用主题。如果您的默认主题使用 AppCompat,则 ProgressBar 的颜色将为您定义的 colorAccent

更改 colorAccent 也会更改 ProgressBar 的颜色,但这些更改也会反映在多个位置。因此,如果您想要为特定的 ProgressBar 使用不同的颜色,您可以通过单独将主题应用于该 ProgressBar 来实现:

  • 扩展默认主题并覆盖 颜色强调

    <样式名称=“AppTheme.WhiteAccent”>
        <项目名称=“colorAccent”>@color/white 
    
    
  • 并在 ProgressBar 中添加 android:theme 属性:< /p>

    android:theme="@style/AppTheme.WhiteAccent"
    

所以它看起来像这样:

<ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:theme="@style/AppTheme.WhiteAccent" />

所以你只是改变一个colorAccent 对于您的特定 ProgressBar

注意:使用style将不起作用。您只需要使用android:theme
您可以在此处找到主题的更多用法:https://plus.google.com /u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

编辑

以下是通过编程方式更改 ProgressBar 颜色的代码。

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));

This is an old question, but using theme is not mentioned here. If your default theme is using AppCompat, your ProgressBar's color will be colorAccent you have defined.

Changing colorAccent will also change your ProgressBar's color, but these changes also reflects at multiple places. So, if you want a different color just for a specific ProgressBar you can do that by applying theme to that ProgressBar alone:

  • Extend your default theme and override colorAccent

    <style name="AppTheme.WhiteAccent">
        <item name="colorAccent">@color/white</item> <!-- Whatever color you want-->
    </style>
    
  • And in ProgressBar add the android:theme attribute:

    android:theme="@style/AppTheme.WhiteAccent"
    

So it will look something like this:

<ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:theme="@style/AppTheme.WhiteAccent" />

So you are just changing a colorAccent for your particular ProgressBar.

Note: Using style will not work. You need to use android: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.

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
猫卆 2024-11-23 15:35:01

转到 .xml 文件中的进度条声明:


粘贴这行代码:

android:indeterminateTint="@color/white"

上面的示例允许您将进度条的颜色更改为 <强>白色。 现在您可以将其调整您想要的颜色

仅适用于 Android API >= 21

Go to the declaration of the progress bar in your .xml file :


And paste this line of code :

android:indeterminateTint="@color/white"

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

凝望流年 2024-11-23 15:35:01

在 xml 中你可以像这样应用颜色

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/loader"
    android:indeterminateTint="@color/my_color"
    android:layout_centerInParent="true"/>

in xml u can apply color like this

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/loader"
    android:indeterminateTint="@color/my_color"
    android:layout_centerInParent="true"/>
梨涡 2024-11-23 15:35:01

对我来说,这两行必须在那里才能工作并更改颜色:

android:indeterminateTint="@color/yourColor"
android:indeterminateTintMode="src_in"

PS:但它仅适用于 android 21

for me, these two lines had to be there for it to work and change the color:

android:indeterminateTint="@color/yourColor"
android:indeterminateTintMode="src_in"

PS: but its only available from android 21

一场信仰旅途 2024-11-23 15:35:01

创建进度可绘制文件

<?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>
        <corners android:radius="5dip" />
        <solid android:color="@color/colorWhite" />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </clip>
</item>

并设置
android:progressDrawable="@drawable/app_progressbar_back"

像这样

<ProgressBar
                android:id="@+id/dialogProgressView"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/app_progressbar_back"
                android:progress="50"
                android:layout_marginTop="@dimen/margin_20" />

create progress drawable file

<?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>
        <corners android:radius="5dip" />
        <solid android:color="@color/colorWhite" />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </clip>
</item>

and set
android:progressDrawable="@drawable/app_progressbar_back"

like this

<ProgressBar
                android:id="@+id/dialogProgressView"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/app_progressbar_back"
                android:progress="50"
                android:layout_marginTop="@dimen/margin_20" />
情绪少女 2024-11-23 15:35:01

您还可以在活动 oncreate 或片段 onViewCreated() 中以编程方式更改进度条的颜色,

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

这里应用的颜色是“0xFFFF0000”,即红色。您可以根据您的需要进行更改

You can also change the color of your progressbar programmatically in your activities oncreate or your fragments onViewCreated()

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

Here the color applied is "0xFFFF0000" which is red. You can change it according to your needs

明月松间行 2024-11-23 15:35:01

这是通过 java 代码实现的方法。

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(getActivity(),R.color.product_status_color), PorterDuff.Mode.MULTIPLY)

here is how you can do it by java code.

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(getActivity(),R.color.product_status_color), PorterDuff.Mode.MULTIPLY)
别理我 2024-11-23 15:35:01

这是一些解释。要更改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 of ProgressBar. If you have a Horizontal ProgressBar then you need a rectangle.

PS: I have not tried with other shapes.

站稳脚跟 2024-11-23 15:35:01

我使用的是安卓2.3.1版本。我创建了一个名为progressbar的xml文件,并在布局进度条标签中调用它 android:progressDrawable="@drawable/progressbar"

<item android:id="@+id/progress">
    <clip>
    <shape>
        <gradient android:startColor="@color/textColor"
            android:centerColor="@color/textColor"
            android:endColor="@color/textColor"
            android:gradientRadius="20dp"
            >
        </gradient>
    </shape>
    </clip>
</item>

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"

<item android:id="@+id/progress">
    <clip>
    <shape>
        <gradient android:startColor="@color/textColor"
            android:centerColor="@color/textColor"
            android:endColor="@color/textColor"
            android:gradientRadius="20dp"
            >
        </gradient>
    </shape>
    </clip>
</item>

只是一片海 2024-11-23 15:35:01

奇拉格的答案是正确的,但并非全部。
我认为 XML 文件应该如下所示:

<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:indeterminateDrawable="@drawable/progress"/>

The Chirag's answer is right, but not all.
I think the XML file should look like this:

<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:indeterminateDrawable="@drawable/progress"/>
很酷不放纵 2024-11-23 15:35:01

使用 android:inminatedDrawable="@drawable/.." 肯定会改变进度条背景的颜色。但就我而言,进展是确定的。

我可以使用进度栏中的以下行更改进度和进度背景的颜色:

<ProgressBar                                 
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="15dp"
    android:progress="50"
    android:progressBackgroundTint="@color/colorWhite"
    android:progressTint="@color/progress_green"
    android:textAlignment="center" />

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:

<ProgressBar                                 
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="15dp"
    android:progress="50"
    android:progressBackgroundTint="@color/colorWhite"
    android:progressTint="@color/progress_green"
    android:textAlignment="center" />
寒冷纷飞旳雪 2024-11-23 15:35:01

对于任何使用 CircularProgressIndicator 的人,您可以尝试:

app:indicatorColor="@color/yourColor"
app:indicatorSize="4dp"

For anyone using CircularProgressIndicator, you can try:

app:indicatorColor="@color/yourColor"
app:indicatorSize="4dp"
酒绊 2024-11-23 15:35:01

您也可以仅更改属性

android:indeterminateDrawable="@drawable/yourxmlfile"

并保留此样式

style="@android:style/Widget.ProgressBar.Small"

you also can change only the attribute

android:indeterminateDrawable="@drawable/yourxmlfile"

and keep this style

style="@android:style/Widget.ProgressBar.Small"
偏爱自由 2024-11-23 15:35:01

尝试在您的 XML 中使用它

android:indeterminateDrawable="@drawable/yourxmlfile"

style="@android:style/Widget.ProgressBar.Small"

Try using this in your XML

android:indeterminateDrawable="@drawable/yourxmlfile"

style="@android:style/Widget.ProgressBar.Small"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文