【问题标题】:How to change default color of progress bar?如何更改进度条的默认颜色?
【发布时间】:2011-09-19 06:06:56
【问题描述】:

我在我的 Activty 中使用了循环 ProgressBar。我的问题是它在我的页面上无法正常显示,因为我的页面的 BG 颜色与 ProgressBar 相同。那么我该如何更改 ProgressBar 的颜色 使其正确可见? 请帮忙

【问题讨论】:

标签: android android-layout progress-bar


【解决方案1】:

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

喜欢

<ProgressBar
  android:id="@+id/ProgressBar01" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background = "@xml/progress">

【讨论】:

  • 谢谢 Chirag 这正是我想要的。
  • 感谢您的回答,对我帮助很大。
  • 了解更多关于styling progress bars here的信息。
  • 我认为它应该在drawable而不是xml
  • 工作很好,绝对是我想要的。
【解决方案2】:

这是一个老问题,但这里没有提到使用主题。如果您的默认主题使用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:theme 属性:

    android:theme="@style/AppTheme.WhiteAccent"
    

所以它看起来像这样:

<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 更改colorAccent

注意:使用style 将不起作用。您只需要使用android:theme。 你可以在这里找到更多主题的使用:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

编辑

这里是通过编程改变 ProgressBar 颜色的代码。

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));

【讨论】:

  • 不错的答案,您甚至可以同时拥有android:theme="@style/AppTheme.WhiteAccent"style="@style/Widget.AppCompat.ProgressBar"
  • 你成就了我的一天。使用android:theme="@style/AppTheme.WhiteAccent" 改变ProgressBar 颜色的技巧
  • 这也可以在整个应用程序中使用,只要您需要白色的accentColor。太棒了!
【解决方案3】:

最简单的方法是使用 android:indeterminateTint 属性:

<ProgressBar
        android:indeterminate="true"
        android:indeterminateTint="@android:color/black"
        ........... />

【讨论】:

  • 从 api 21 开始支持。
  • 我认为这是最好的答案!
【解决方案4】:

转到 .xml 文件中进度条的声明:


然后粘贴这行代码:

android:indeterminateTint="@color/white"

上面的示例允许您将进度条的颜色更改为白色现在由您来调整它以适应您想要的颜色

仅适用于 Android API >= 21

【讨论】:

    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

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

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

      PS:但它仅适用于 android 21

      【讨论】:

        【解决方案7】:

        创建进度可绘制文件

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

        喜欢这个

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

        【讨论】:

          【解决方案8】:

          您还可以在活动 oncreate 或片段 onViewCreated() 中以编程方式更改进度条的颜色

          pBar.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
          

          这里应用的颜色是红色的“0xFFFF0000”。你可以根据自己的需要改变它

          【讨论】:

            【解决方案9】:

            这里是如何通过 java 代码来实现的。

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

            【讨论】:

            • 如果getProgressDrawable() 返回 null 怎么办?
            • 这在棒棒糖之前的设备上对我有用,它只能在 KitKat 设备之后为空。对于棒棒糖设备后,我有类似的东西。 progress.getIndeterminateDrawable().setColorFilter(Color.RED, Mode.MULTIPLY);我检查了构建操作系统版本并应用了相应的代码。
            【解决方案10】:

            这里有一些解释。要更改Indeterminate ProgressBar 的背景,您需要一个旋转环,因为它会旋转。在@Chirag 的回答中,他定义了一个旋转环并将其应用于ProgressBar 的背景。如果您有Horizontal ProgressBar,那么您需要rectangle

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

            【讨论】:

              【解决方案11】:

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

              <item android:id="@+id/progress">
                  <clip>
                  <shape>
                      <gradient android:startColor="@color/textColor"
                          android:centerColor="@color/textColor"
                          android:endColor="@color/textColor"
                          android:gradientRadius="20dp"
                          >
                      </gradient>
                  </shape>
                  </clip>
              </item>
              

              【讨论】:

                【解决方案12】:

                Chirag 的回答是正确的,但不是全部。
                我认为 XML 文件应该是这样的:

                <ProgressBar
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:indeterminateDrawable="@drawable/progress"/>
                

                【讨论】:

                  【解决方案13】:

                  你也可以只改变属性

                  android:indeterminateDrawable="@drawable/yourxmlfile"
                  

                  并保持这种风格

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

                  【讨论】:

                    【解决方案14】:

                    尝试在您的 XML 中使用它

                    android:indeterminateDrawable="@drawable/yourxmlfile"
                    
                    style="@android:style/Widget.ProgressBar.Small"
                    

                    【讨论】:

                      猜你喜欢
                      • 2018-05-27
                      • 2016-11-02
                      • 2014-10-08
                      • 1970-01-01
                      • 2022-01-02
                      • 1970-01-01
                      • 2014-06-12
                      • 1970-01-01
                      相关资源
                      最近更新 更多