【问题标题】:How to set the secondary color of determinate circular progress bar如何设置确定圆形进度条的辅助颜色
【发布时间】:2019-02-11 05:58:08
【问题描述】:

我想更改确定进度条的主要颜色和次要颜色,分别为绿色和灰色。 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"
        />

【问题讨论】:

  • 发布您的代码以及问题。
  • 我已经更新了代码
  • 为 ProgressBar 创建自定义进度可绘制对象。
  • @MushahidGillani 参考这个答案stackoverflow.com/a/2021119/2772969
  • @Prashant 抱歉,它不起作用

标签: android android-layout android-xml android-progressbar


【解决方案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>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                  <corners android:radius="5dip" />
                  <solid android:color="@color/secondaryColor"/>
            </shape>
        </clip>
    </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"/>

【讨论】:

  • 我怎样才能使边缘像上图那样圆润
  • 对不起。我刚看了你的问题。我在顶部编辑了我的答案))你可以看看!
【解决方案2】:

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

在 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_shapexml 的位置

<?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_barxml

<?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>

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 2011-07-17
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2023-04-01
    • 2016-06-03
    • 2021-08-30
    相关资源
    最近更新 更多