如何设置确定的圆形进度条的辅助颜色

时间:2019-02-11 05:58:09

标签: android android-layout android-xml

circular progress bar

我想更改确定进度条的原色和副色,分别为绿色和灰色。 Android默认进度条甚至不显示次要进度。我怎样才能做到这一点?谢谢!

<ProgressBar
        android:id="@+id/progress_circular"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:indeterminate="false"
        style="?android:attr/progressBarStyleLarge"
        android:layout_marginTop="18dp"
        app:layout_constraintTop_toBottomOf="@id/textView2"
        app:layout_constraintStart_toStartOf="@id/mainView"
        android:progress="50"
        app:layout_constraintEnd_toEndOf="@id/mainView"
        />

2 个答案:

答案 0 :(得分:1)

您可以创建自定义进度条样式。

  

custom_progress.xml

<?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
              android:innerRadiusRatio="2.8"
              android:shape="ring"
              android:useLevel="false"
              android:type="sweep"
              android:thicknessRatio="18.0">
              <solid android:color="@color/red"/>
         </shape>
    </item>
    <item android:id="@android:id/progress">
        <rotate
              android:pivotX="50%"
              android:pivotY="50%"
              android:fromDegrees="-90"
              android:toDegrees="-90">
              <shape
                  android:innerRadiusRatio="2.8"
                  android:shape="ring"
                  android:angle="0"
                  android:type="sweep"
                  android:thicknessRatio="18.0">
                  <solid android:color="@color/blue"/>

            </shape>
        </rotate>
    </item>
</layer-list>

和xml中的

 <ProgressBar
      android:layout_centerInParent="true"
      android:id="@+id/winRateProgressBar"
      android:layout_width="48dp"
      android:layout_height="48dp"
      style="?android:attr/progressBarStyleHorizontal"
      android:indeterminate="false"
      android:max="100"
      android:progress="20"
      android:progressDrawable="@drawable/custom_progress"
      />

答案 1 :(得分:0)

  

要自定义进度条,请执行此操作

在xml中。

<ProgressBar
            android:id="@+id/progress_circular"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="256dp"
            android:layout_height="256dp"
            android:layout_marginTop="18dp"
            app:layout_constraintTop_toBottomOf="@id/textView2"
            app:layout_constraintStart_toStartOf="@id/mainView"
            android:progress="50"
            app:layout_constraintEnd_toEndOf="@id/mainView"
            android:background="@drawable/circle_shape"
            android:indeterminate="false"
            android:progressDrawable="@drawable/circular_progress_bar" />

其中circle_shape xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="2.5"
    android:thickness="1dp"
    android:useLevel="false">

    <solid android:color="#CCC" /> // use your color here 

</shape>

circular_progress_bar xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="270"
    android:toDegrees="270">
    <shape
        android:innerRadiusRatio="2.5"
        android:shape="ring"
        android:thickness="2dp"
        android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->

        <gradient
            android:angle="0"
            android:endColor="#ffffff"
            android:startColor="#ffffff"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>