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

Android 如何更改进度条的默认颜色?,android,android-layout,progress-bar,Android,Android Layout,Progress Bar,我在我的活动中使用了一个循环的ProgressBar。我的问题是它在我的页面上不能正常显示,因为我的页面的BG颜色与ProgressBar相同。那么我如何更改ProgressBar的颜色以使其正确可见? 请帮助请创建一个xml文件名progress.xml并将其放入res/xml文件夹中,然后在该xml文件中编写以下代码 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.a

我在我的活动中使用了一个循环的
ProgressBar
。我的问题是它在我的页面上不能正常显示,因为我的页面的BG颜色与ProgressBar相同。那么我如何更改ProgressBar的颜色以使其正确可见?
请帮助

请创建一个xml文件名progress.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文件后,将progressbars背景设置为此xml

喜欢
您也只能更改属性

android:indeterminateDrawable="@drawable/yourxmlfile"
保持这种风格

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

试着在XML中使用它

android:indeterminateDrawable="@drawable/yourxmlfile"

style="@android:style/Widget.ProgressBar.Small"
最简单的方法是使用 android:不确定属性:

<ProgressBar
        android:indeterminate="true"
        android:indeterminateTint="@android:color/black"
        ........... />
android:theme="@style/AppTheme.WhiteAccent"

下面是如何通过java代码实现的

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

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

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

  • 扩展默认主题并覆盖
    colorAccent

    <style name="AppTheme.WhiteAccent">
        <item name="colorAccent">@color/white</item> <!-- Whatever color you want-->
    </style>
    
所以它看起来像这样:

<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" />
<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:indeterminateDrawable="@drawable/progress"/>

这里有一些解释。要更改不确定进度条的背景,您需要一个旋转环,因为它会旋转。在@Chirag的回答中,他定义了一个旋转环,并将其应用于
ProgressBar
的背景。如果您有一个
水平进度条
,则需要一个
矩形


PS:我还没有尝试过其他形状。

在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"/>

创建进度可绘制文件

<?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\u progressbar\u back”

像这样

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/loader"
    android:indeterminateTint="@color/my_color"
    android:layout_centerInParent="true"/>
<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" />

您还可以在activities oncreate或fragments onViewCreated()中以编程方式更改progressbar的颜色


此处应用的颜色为红色的“0xFFFF0000”。您可以根据需要更改它

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



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

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

PS:但是它只能从android 21上获得,Chirag的答案是正确的,但不是全部。
我认为XML文件应该如下所示:

<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" />
<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:indeterminateDrawable="@drawable/progress"/>

转到.xml文件中进度条的声明:
然后粘贴这行代码:

android:indeterminateTint="@color/white"
上面的示例允许您将进度条的颜色更改为白色现在由您来调整使其适应您想要的颜色


仅适用于Android API>=21

谢谢Chirag,这正是我想要的。谢谢你的回答,它确实帮了我很多。了解更多信息。我认为它应该是可绘制的,而不是xmlnice,工作正常绝对是我想要的。请尝试解决方案,这里可能是我从[如果
getProgressDrawable()
返回空值,这个链接对你有用吗?这对我来说在棒棒糖前的设备上是有效的,只有在KitKat设备上才可以为空。对于棒棒糖后的设备,我有这样的东西。progress.getIndenderedRawable().setColorFilter(Color.RED,Mode.MULTIPLY);我已经检查了构建操作系统版本并应用了相应的代码。回答很好,你甚至可以使用
android:theme=“@style/AppTheme.WhiteAccent”
style=“@style/Widget.AppCompat.ProgressBar”
来同时使用
android:theme=“@style/AppTheme.WhiteAccent”
使用更改
ProgressBar
color的技巧这也可以在整个应用程序中使用,无论您需要白色的accentColor。好极了!api 21及更高版本支持这一点。我认为这是最好的答案!
android:indeterminateTint="@color/white"