【问题标题】:How to change ProgressBar's progress indicator color in Android如何在 Android 中更改进度条进度指示器颜色
【发布时间】:2023-03-29 03:40:01
【问题描述】:

我已经设置了水平ProgressBar

我想将进度颜色更改为黄色。

<ProgressBar 
    android:id="@+id/progressbar" 
    android:layout_width="80dip" 
    android:layout_height="20dip"  
    android:focusable="false" 
    style="?android:attr/progressBarStyleHorizontal" />

问题是,不同设备的进度颜色不同。 所以,我希望它修复进度颜色。

【问题讨论】:

  • 以上答案改变了整个背景颜色,并且将进度条的高度更改为最大。无法在 xml 中更改。仅更改进度条状态的更好方法是完成多少是 20% 红色,50% 黄色 70% 及以上绿色。你可以在java中以编程方式完成。如果您有任何其他解决方案,请分享您的答案。

标签: android android-progressbar


【解决方案1】:

通过材质组件库,您可以使用 LinearProgressIndicatorapp:indicatorColor 属性来更改颜色:

  <com.google.android.material.progressindicator.LinearProgressIndicator
      app:indicatorColor="@color/..."
      app:trackThickness="..."
      app:trackColor="@color/..."
      />

注意:至少需要1.3.0-alpha04的版本。

使用 ProgressBar 您可以使用以下方法覆盖颜色:

<ProgressBar
    android:theme="@style/CustomProgressBarTheme"
    ..>

与:

<style name="CustomProgressBarTheme">
    <item name="colorAccent">@color/primaryDarkColor</item>
</style>

【讨论】:

  • 我建议使用它而不是标准的 ProgressBar,因为它允许您在保持材质动画的同时更改轨道颜色。
【解决方案2】:

只需将android:indeterminateTint="@color/colorPrimary" 添加到您的进度条

例子:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateTint="@color/colorPrimary"/>

如果progressBarStyleHorizo​​ntal改变android:progressTint="@color/colorPrimary"

例子:

 <ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:progress="50"
    android:progressTint="@color/colorPrimary" />

【讨论】:

    【解决方案3】:

    您可以使用自定义可绘制来使您的进度条样式化。创建可绘制文件

    progress_user_badge.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:shape="rectangle">
                <corners android:radius="@dimen/_5sdp"/>
                <solid android:color="@color/your_default_color" />
            </shape>
        </item>
    
        <item android:id="@android:id/progress">
            <clip>
                <shape android:shape="rectangle">
                    <corners android:radius="@dimen/_5sdp"/>
                    <solid android:color="@color/your_exact_color" />
                </shape>
            </clip>
        </item>
    
    </layer-list>
    

    现在在您的小部件中使用这个可绘制文件

    <ProgressBar
        android:id="@+id/badge_progress_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="@dimen/_200sdp"
        android:layout_height="@dimen/_7sdp"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/_15sdp"
        android:indeterminate="false"
        android:max="100"
        android:progress="70"
        android:progressDrawable="@drawable/progress_user_badge"/>
    

    您可以使用下面的代码以编程方式更改 Progressbar 的进度颜色

    badge_progress_bar.progressTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.your_color))
    

    【讨论】:

      【解决方案4】:

      如果您想在android中以编程方式将主要和次要进度颜色设置为水平进度条,请使用下面的sn-p

      int[][] states = new int[][] {
                              new int[] {-android.R.attr.state_checked},
                              new int[] {android.R.attr.state_checked},
                      };
      
                      int[] secondaryColor = new int[] {
                              context.getResources().getColor(R.color.graph_line_one),
                              Color.RED,
                      };
      
                      int[] primaryColor = new int[] {
                              context.getResources().getColor(R.color.middle_progress),
                              Color.BLUE,
                      };
                      progressbar.setProgressTintList(new ColorStateList(states, primaryColor));
                      progressbar.setSecondaryProgressTintList(new ColorStateList(states, secondaryColor));
      

      【讨论】:

        【解决方案5】:

        在xml中:

        <ProgressBar
                            android:id="@+id/progressBar"
                            style="?android:attr/progressBarStyleInverse"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_margin="@dimen/dimen_10dp"
        
        
                         android:indeterminateTint="@{viewModel.getComplainStatusUpdate(position)}"
                            android:indeterminate="true"
                            android:indeterminateOnly="false"
                            android:max="100"
                            android:clickable="false"
                            android:indeterminateDrawable="@drawable/round_progress"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
        

        视图模型:

        fun getComplainStatusUpdate(position: Int):Int{
        
                        val list = myAllComplainList!!.getValue()
                        if (list!!.get(position).complainStatus.equals("P")) {
                          //  progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
                           // progressBar.setBackgroundResource(R.drawable.circle_shape)
                            return Color.RED
                        } else if (list!!.get(position).complainStatus.equals("I")) {
        
                            return Color.GREEN
                        } else {
        
                            return Color.GREEN
                        }
        
        
        return Color.CYAN
            }
        

        【讨论】:

          【解决方案6】:

          我从我的一个应用程序中复制了这个,所以可能有一些额外的属性,但应该会给你这个想法。这是来自有进度条的布局:

          <ProgressBar
              android:id="@+id/ProgressBar"
              style="?android:attr/progressBarStyleHorizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:indeterminate="false"
              android:maxHeight="10dip"
              android:minHeight="10dip"
              android:progress="50"
              android:progressDrawable="@drawable/greenprogress" />
          

          然后创建一个类似于以下内容的新drawable(在本例中为greenprogress.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>
                      <corners android:radius="5dip" />
                      <gradient
                          android:angle="270"
                          android:centerColor="#ff5a5d5a"
                          android:centerY="0.75"
                          android:endColor="#ff747674"
                          android:startColor="#ff9d9e9d" />
                  </shape>
              </item>
          
              <item android:id="@android:id/secondaryProgress">
                  <clip>
                      <shape>
                          <corners android:radius="5dip" />
                          <gradient
                              android:angle="270"
                              android:centerColor="#80ffb600"
                              android:centerY="0.75"
                              android:endColor="#a0ffcb00"
                              android:startColor="#80ffd300" />
                      </shape>
                  </clip>
              </item>
              <item android:id="@android:id/progress">
                  <clip>
                      <shape>
                          <corners android:radius="5dip" />
                          <gradient
                              android:angle="270"
                              android:endColor="#008000"
                              android:startColor="#33FF33" />
                      </shape>
                  </clip>
              </item>
          
          </layer-list>
          

          您可以根据需要更改颜色,这会给您一个绿色的进度条。

          【讨论】:

          • 我也删除了这个 android:indeterminate="false"
          • 请帮帮我。我尝试通过复制您的代码来创建 greenprogress.xml,但 Android Studio 0.4.6 将 标记为并且错误“必须声明元素列表”我该怎么办?
          • 可以动态改变颜色吗?
          • 可以根据实际bar中定义的当前进度值改变颜色吗?
          • @Ryan 解释一下android:id="@android:id/progress"android:id="@android:id/background"android:id="@android:id/secondaryProgress" 代表什么会很有帮助。
          【解决方案7】:
          final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
          layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
          

          【讨论】:

          • 尝试在您的答案中添加一点解释。不鼓励仅使用代码回答。
          • 我很想得到 getDrawable 中索引 2 背后的解释,但至少这是唯一对我有用的解决方案,所以谢谢!
          【解决方案8】:

          创建一个你需要什么背景的可绘制资源,在我的例子中,我将它命名为 bg_c​​ustom_progressbar.xml

          <?xml version="1.0" encoding="utf-8"?>
          

          <item>
              <shape android:shape="rectangle" >
                  <corners android:radius="5dp"/>
          
                  <gradient
                      android:angle="180"
                      android:endColor="#DADFD6"
                      android:startColor="#AEB9A3" />
              </shape>
          </item>
          <item>
              <clip>
                  <shape android:shape="rectangle" >
                      <corners android:radius="5dp" />
          
                      <gradient
                          android:angle="180"
                          android:endColor="#44CF4A"
                          android:startColor="#2BB930" />
                  </shape>
              </clip>
          </item>
          <item>
              <clip>
                  <shape android:shape="rectangle" >
                      <corners android:radius="5dp" />
          
                      <gradient
                          android:angle="180"
                          android:endColor="#44CF4A"
                          android:startColor="#2BB930" />
                  </shape>
              </clip>
          </item>
          

          然后像这样使用自定义背景

           <ProgressBar
                          android:id="@+id/progressBarBarMenuHome"
                          style="?android:attr/progressBarStyleHorizontal"
                          android:layout_width="match_parent"
                          android:layout_height="20dp"
                          android:layout_marginStart="10dp"
                          android:layout_marginEnd="10dp"
                          android:layout_marginBottom="6dp"
                          android:indeterminate="false"
                          android:max="100"
                          android:minWidth="200dp"
                          android:minHeight="50dp"
                          android:progress="10"
                          android:progressDrawable="@drawable/bg_custom_progressbar" />
          

          它对我来说很好用

          【讨论】:

            【解决方案9】:

            如果您处于 API 级别 21 或更高级别,则可以使用这些 XML 属性:

            <!-- For indeterminate progress bar -->
            android:indeterminateTint="@color/white"
            <!-- For normal progress bar -->
            android:progressTint="@color/white"
            

            【讨论】:

              【解决方案10】:

              只需在values/styles.xml 中创建一个样式。

              <style name="ProgressBarStyle">
                  <item name="colorAccent">@color/greenLight</item>
              </style>
              

              然后将此样式设置为您的ProgressBar 主题。

              <ProgressBar
                  android:theme="@style/ProgressBarStyle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"/>
              

              不管你的进度条是水平的还是圆形的。 就是这样。

              【讨论】:

              • 哇,最简单的解决方案对我有用。我想知道为什么这个答案有这么多票。
              【解决方案11】:

              有3种方法可以解决这个问题:

              1. 如何以声明方式调整进度条颜色
              2. 如何以编程方式调整进度条颜色,但从编译时声明的各种预定颜色中选择颜色
              3. 如何以编程方式调整进度条颜色,以及以编程方式创建颜色。

              特别是在 #2 和 #3 周围存在很多混淆,如 cmets 对 amfcosta 的回答所示。每当您想将进度条设置为除原色以外的任何颜色时,该答案都会产生不可预测的颜色结果,因为它只会修改背景颜色,而实际的进度条“剪辑”区域仍将是黄色叠加层,不透明度降低。例如,使用该方法将背景设置为深紫色会导致进度条“剪辑”颜色为某种疯狂的粉红色,这是由于深紫色和黄色通过降低 alpha 混合而产生的。

              所以无论如何,Ryan 完美地回答了 #1,而 Štarke 回答了 #3 的大部分问题,但对于那些寻求 #2 和 #3 的完整解决方案的人来说:


              如何以编程方式调整进度条颜色,但从 XML 中声明的预定颜色中选择颜色

              res/drawable/my_progressbar.xml

              创建此文件,但更改 my_progress 和 my_secondsProgress 中的颜色:

              (注意:这只是定义默认android SDK ProgressBar的实际XML文件的副本,但我已经更改了ID和颜色。原始文件名为progress_horizo​​ntal)

              <?xml version="1.0" encoding="utf-8"?>
              <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
              
              <item android:id="@+id/my_progress_background">
                  <shape>
                      <corners android:radius="5dip" />
                      <gradient
                          android:startColor="#ff9d9e9d"
                          android:centerColor="#ff5a5d5a"
                          android:centerY="0.75"
                          android:endColor="#ff747674"
                          android:angle="270"
                          />
                  </shape>
              </item>
              
              <item android:id="@+id/my_secondaryProgress">
                  <clip>
                      <shape>
                          <corners android:radius="5dip" />
                          <gradient
                              android:startColor="#80ff171d"
                              android:centerColor="#80ff1315"
                              android:centerY="0.75"
                              android:endColor="#a0ff0208"
                              android:angle="270"
                              />
                      </shape>
                  </clip>
              </item>
              
              <item android:id="@+id/my_progress">
                  <clip>
                      <shape>
                          <corners android:radius="5dip" />
                          <gradient
                              android:startColor="#7d2afdff"
                              android:centerColor="#ff2afdff"
                              android:centerY="0.75"
                              android:endColor="#ff22b9ba"
                              android:angle="270"
                              />
                      </shape>
                  </clip>
              </item>
              

              在您的 Java 中

              final Drawable drawable;
              int sdk = android.os.Build.VERSION.SDK_INT;
                  if(sdk < 16) {
                      drawable =  ctx.getResources().getDrawable(R.drawable.my_progressbar);
                  } else {
                      drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
                  }
              progressBar.setProgressDrawable(drawable)
              

              如何以编程方式调整进度条颜色,以及以编程方式创建颜色

              编辑:我找到了正确解决这个问题的时间。我以前的回答有点模棱两可。

              ProgressBar 由一个 LayerDrawable 中的 3 个 Drawable 组成。

              1. 第 1 层是背景
              2. 第 2 层是次要进度颜色
              3. 第 3 层是主要的进度条颜色

              在下面的示例中,我将主进度条的颜色更改为青色。

              //Create new ClipDrawable to replace the old one
              float pxCornerRadius = viewUtils.convertDpToPixel(5);
              final float[] roundedCorners = new float[] { pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius };
              ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
              shpDrawable.getPaint().setColor(Color.CYAN);
              final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
              
              //Replace the existing ClipDrawable with this new one
              final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
              layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);
              

              该示例将其设置为纯色。您可以通过替换将其设置为渐变色

              shpDrawable.getPaint().setColor(Color.CYAN);
              

              Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
              shpDrawable.getPaint().setShader(shader);
              

              干杯。

              -兰斯

              【讨论】:

              • viewUtils.convertDpToPixel(5)中的viewUtils是什么;
              • 嗨@NikosHidalgo - 你可以在这里看到一个几乎相同的方法:stackoverflow.com/questions/12849366/…。唯一的区别是我构造我的ViewUtils 引用Context,这样我在调用convertDpToPixel() 时不需要传递Context
              • 感谢您的及时回复,尤其是很久以前发布的答案!
              • 哈哈,我真的不得不绞尽脑汁才能记住:p
              【解决方案12】:

              对于 API 级别 21 或更高级别,只需使用

              android:progressBackgroundTint="@color/yourBackgroundColor"
              android:progressTint="@color/yourColor"
              

              【讨论】:

                【解决方案13】:

                ProgressBar 颜色可以改变如下:

                /res/values/colors.xml

                <?xml version="1.0" encoding="utf-8"?>
                <resources>
                    <color name="colorAccent">#FF4081</color>
                </resources>
                

                /res/values/styles.xml

                <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                    <item name="colorAccent">@color/colorAccent</item>
                </style> 
                

                创建:

                progressBar = (ProgressBar) findViewById(R.id.progressBar);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
                    drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
                    progressBar.setIndeterminateDrawable(drawable);
                }
                

                【讨论】:

                  【解决方案14】:

                  你们真让我头疼。 您可以做的是 首先通过 xml 使您的图层列表可绘制(意味着背景作为第一层,可绘制表示次要进度作为第二层,另一个可绘制表示主要进度为最后一层),然后通过执行以下操作更改代码上的颜色:

                  public void setPrimaryProgressColor(int colorInstance) {
                        if (progressBar.getProgressDrawable() instanceof LayerDrawable) {
                          Log.d(mLogTag, "Drawable is a layer drawable");
                          LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
                          Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
                          circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
                          progressBar.setProgressDrawable(layered);
                      } else {
                          Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
                          progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
                      }
                  }
                  

                  此方法将为您提供最大的灵活性,让您在 xml 上声明原始可绘制对象的测量,之后无需修改其结构,特别是如果您需要特定于 xml 文件屏幕文件夹,那么只需修改通过代码颜色。无需从头开始重新实例化新的 ShapeDrawable。

                  【讨论】:

                    【解决方案15】:

                    对于任何寻找如何以编程方式进行操作的人:

                        Drawable bckgrndDr = new ColorDrawable(Color.RED);
                        Drawable secProgressDr = new ColorDrawable(Color.GRAY);
                        Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
                        LayerDrawable resultDr = new LayerDrawable(new Drawable[] { bckgrndDr, secProgressDr, progressDr });
                        //setting ids is important
                        resultDr.setId(0, android.R.id.background);
                        resultDr.setId(1, android.R.id.secondaryProgress);
                        resultDr.setId(2, android.R.id.progress);
                    

                    将 ids 设置为 drawables 至关重要,并负责保留进度条的边界和​​实际状态

                    【讨论】:

                      【解决方案16】:

                      我发现的最简单的解决方案是这样的:

                      <item name="colorAccent">@color/white_or_any_color</item>
                      

                      将以上内容放入res &gt; values文件夹下的styles.xml文件中。

                      注意:如果您使用任何其他强调色,那么之前的解决方案应该很好用。

                      API 21 或更高版本

                      【讨论】:

                      • 这是最好的解决方案。 100% 与提出的问题相关。很简单
                      【解决方案17】:

                      如果你只想改变进度条的颜色,你可以简单地在你的Activity的onCreate()方法中使用一个颜色过滤器:

                      ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
                      int color = 0xFF00FF00;
                      progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
                      progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
                      

                      来自here的想法。

                      您只需要对棒棒糖之前的版本执行此操作。在 Lollipop 上,您可以使用 colorAccent 样式属性。

                      【讨论】:

                      • SeekBar 相同,只需为 API 16 添加:seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);。这将改变那个圆形拇指的颜色。
                      • 直接在可绘制对象上调用setColorFilter() 将在任何地方应用颜色。由于这是我不需要的,我只是在Drawable 上调用了mutate()(例如seekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
                      • 我在其他地方看到过这个解决方案,但它所做的(在 Gingerbread 设备上测试)只是将整个进度条变成一种颜色,从而无法看到实际进度
                      • @NeilC.Obremski 是的,你是对的。它可能是特定于 ROM 的,也可能是特定于版本的。我不得不完全删除它。希望我们能尽快将它移植回旧版本的 Android(通过支持库)。
                      【解决方案18】:

                      一个更简单的解决方案:

                      progess_drawable_blue

                      <?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>
                              <solid
                                      android:color="@color/disabled" />
                          </shape>
                      </item>
                      
                      <item
                          android:id="@android:id/progress">
                          <clip>
                              <shape>
                                  <solid
                                      android:color="@color/blue" />
                              </shape>
                          </clip>
                      </item>
                      
                      </layer-list>
                      

                      【讨论】:

                      • 这里的@color/disabled 是自定义颜色
                      • ,甚至
                      • 这对于 API 杠杆 21 及以上的 android:indeterminateTint="@color/white" 已经足够了
                      猜你喜欢
                      • 2011-01-02
                      • 2016-11-02
                      • 2012-04-17
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-08-26
                      • 2014-10-08
                      相关资源
                      最近更新 更多