【问题标题】:How to change progress bar progress color?如何更改进度条进度颜色?
【发布时间】:2016-11-02 08:52:19
【问题描述】:

我为我的应用程序创建了这个进度条,但我无法获得看起来会变成红色或蓝色的黄色橙色。

<ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="250sp"
        android:layout_height="wrap_content"
        android:progress="2"
        android:layout_marginTop="82dp"
        android:max="3"
        android:indeterminate="false"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

以上是我的进度条xml代码。

有帮助吗? :)

提前非常感谢! :)

【问题讨论】:

标签: android styles android-progressbar


【解决方案1】:

如果android版本是5.0及以上你只需要设置

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

低版本我用这个

mProgressBar.getIndeterminateDrawable().setColorFilter(getResources()
.getColor(R.color.primary_color),PorterDuff.Mode.SRC_IN);

【讨论】:

    【解决方案2】:

    在您的可绘制文件夹中创建一个名为 custom_progress_bar.xml 的新 XML 文件并将其粘贴到那里:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Define the background properties like color etc -->
        <item android:id="@android:id/background">
            <clip>
                <shape>
                    <solid android:color="#000000" />
                </shape>
            </clip>
        </item>
        <!-- Define the progress properties like start color, end color etc -->
        <item android:id="@android:id/progress">
            <clip>
                <shape>
                    <solid android:color="#FFFFFF" />
                </shape>
            </clip>
        </item>
    
    </layer-list>
    

    并在您的&lt;ProgressBar&gt; 中添加此属性:

    android:progressDrawable="@drawable/custom_progress_bar"
    

    根据自己的喜好更改 2 种颜色。我把#FFFFFF#000000 放在了白色和黑色之间。

    【讨论】:

    • 亲爱的 Vuko,您的方法效果惊人!谢谢你的帮助! :)
    • 我想以编程方式做同样的事情。就我而言,我想使用十六进制值从代码中更改进度颜色而不更改背景颜色。需要帮助。
    • 我想到的是,您可以创建具有不同颜色的相同可绘制对象,并从代码中替换进度可绘制对象。如果您需要动态选择颜色(它并不总是相同),那就更难了,如果我想到一个解决方案,我会告诉你:D @NavjotBedi
    【解决方案3】:

    转到您的 styles.xml 并从您的基本应用程序主题更改 colorAccent 值,例如
    &lt;item name="colorAccent"&gt;{COLOR}&lt;/item&gt;
    我正在使用 minSdkVersion 15,它对我有用。

    【讨论】:

    • 这只是一个很好的解决方案,如果你不介意你的颜色在任何地方都可能会变成这个。 ColorAccent 是一个非常重要的属性,应谨慎更改。检查this
    • 它可能看起来比我的解决方案更容易添加这一行人员,但它带有一些警告。使用前请注意。
    【解决方案4】:

    如果你想改变 ProgressBar 颜色,不是通过 drawable,你可以使用这个样式作为 ProgressBar 的主题

    <style name="ProgressBar" parent="Widget.AppCompat.ProgressBar">
        <item name="colorAccent">@color/white_primary</item>
    </style>
    

    【讨论】:

      【解决方案5】:

      试试这个并为你添加自定义颜色

      Progressbar mBar= (ProgressBar) findViewById(R.id.spinner);
      mBar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#80DAEB"),
                  android.graphics.PorterDuff.Mode.MULTIPLY);
      

      希望对你有帮助

      【讨论】:

        【解决方案6】:

        如果android:indeterminateTint 不起作用,试试这个:

        <ProgressBar
            android:id="@+id/progress_bar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:progressTint="@color/Red" <--------------------------
            android:progressBackgroundTint="@color/Blue" <---------------
            android:layout_width="250sp"
            android:layout_height="wrap_content"
            android:progress="2"
            android:layout_marginTop="82dp"
            android:max="3"
            android:indeterminate="false"
            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true" />
        

        【讨论】:

          【解决方案7】:

          供日后参考:

          对于 API 21+,我们可以直接在 XML 中使用:

          android:indeterminateTint="@android:color/white"
          

          【讨论】:

            【解决方案8】:

            像这样在 style.xml 中设置主题

             <style name="ProgressBar" parent="Widget.AppCompat.ProgressBar">
                <item name="colorAccent">@color/dark_blue</item>
            </style>
            

            然后像这样在progressBar中使用这种风格作为“主题”,

            <ProgressBar
                android:id="@+id/progressBar"
                android:theme="@style/ProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="440dp"
                app:layout_constraintEnd_toEndOf="@+id/splashTitle"
                app:layout_constraintHorizontal_bias="0.493"
                app:layout_constraintStart_toStartOf="@+id/splashTitle"
                app:layout_constraintTop_toBottomOf="@+id/splashTitle" />
            

            愉快的编码

            【讨论】:

              猜你喜欢
              • 2014-10-08
              • 1970-01-01
              • 2011-01-02
              • 1970-01-01
              • 1970-01-01
              • 2014-03-27
              • 2014-06-12
              相关资源
              最近更新 更多