如何在圆形进度条中改变颜色?

178

我在Android上使用圆形进度条,希望改变它的颜色。我正在使用

"?android:attr/progressBarStyleLargeInverse" 

如何更改进度条的颜色?需要怎样自定义样式呢?此外,“style”在这里的定义是什么?

23个回答

11

请查看这个答案

对于我来说,为了让它起作用并改变颜色,这两行必须存在:

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

PS:但仅适用于 Android 21 及以上版本


1
唯一最好的最简单解决方案 - Vitaly

10

对我来说,主题在使用accentColor时无法工作。但是它可以使用colorControlActivated。

    <style name="Progressbar.White" parent="AppTheme">
        <item name="colorControlActivated">@color/white</item>
    </style>

  <ProgressBar
        android:layout_width="@dimen/d_40"
        android:layout_height="@dimen/d_40"
        android:indeterminate="true"
        android:theme="@style/Progressbar.White"/>

10
<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>


<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="250dp"
    android:theme="@style/progressColor"
    android:layout_height="250dp"
    android:layout_centerInParent="true" />

4

补充Muhamed Riyas M的最受欢迎答案:

更快的旋转

android:toDegrees="1080"

更细的环
android:thicknessRatio="16"

浅白色

android:endColor="#80ffffff"

4

colorControlActivated主题项添加到您的活动主题中,例如:

    <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
            ...
            <item name="colorControlActivated">@color/rocket_black</item>
            ...
    </style>

在清单文件中将此样式应用于您的Activity:

<activity
    android:name=".packege.YourActivity"
    android:theme="@style/AppTheme.NoActionBar"/>

4

尝试使用样式并将colorControlActivated设置为所需的ProgressBar颜色。

<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
    <item name="colorControlActivated">@color/COLOR</item>
</style>

接着将ProgressBar的主题设置为新样式。

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/progressColor"
 />

3
您可以使用以下样式来更改进度条的颜色:

您可以使用以下样式来更改进度条的颜色:

 <style name="AppTheme.anyName">
        <item name="colorAccent">YOUR_COLOR</item>
    </style>

并在ProgressBar中像下面这样使用它。
<ProgressBar
            style="?android:attr/progressBarStyle"
            android:theme="@style/AppTheme.WhiteAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/dimen_30"
        />

希望可以帮到你。

2
您可以通过定制对话框来轻松实现此操作,可重复使用其他答案中的xml代码:
<?xml version="1.0" encoding="utf-8"?>

<shape
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thicknessRatio="8"
    android:useLevel="false" >

    <size
        android:height="76dip"
        android:width="76dip" />

    <gradient
        android:angle="0"
        android:endColor="@color/oceanBlue"
        android:startColor="@android:color/transparent"
        android:type="sweep"
        android:useLevel="false" />

</shape>

只需要这样做:

public static class ModifiedProgressDialog extends ProgressDialog {
    public ModifiedProgressDialog(Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        setIndeterminateDrawable(getContext().getResources().getDrawable(R.drawable.blue_progress));
    }
}

1
对于所有想知道如何提高自定义进度条速度的人们
  <?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="1080"><!--HERE YOU COULD INCREASE SPEED BY SETTING TODEGRESS(1080 is 3 loops instead of 1 in same amt of time)-->
<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>


1

android:indeterminateDrawable="@drawable/progress_custom_rotate"设置为:

使用以下代码自定义圆形进度条:

复制下面的代码并在Drawable文件夹中创建"progress_custom_rotate.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="1080">

    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">

        <size android:width="48dip" android:height="48dip" />

        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#4c737373" android:centerColor="#4c737373"
            android:centerY="0.50" android:endColor="#ffffd300" />

    </shape>

</rotate>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接