【问题标题】:How to change android indeterminate ProgressBar color?如何更改android不确定的ProgressBar颜色?
【发布时间】:2011-02-07 22:50:29
【问题描述】:

我想知道如何将不确定的ProgressBar 颜色从基础白色/灰色更改为黑色?当我更改indeterminateDrawable 时,我得到的是静态图像而不是移动的动画进度条。有没有办法简单地在 XML 中做到这一点?

【问题讨论】:

    标签: android android-progressbar


    【解决方案1】:

    通过材质组件库,您可以使用具有以下属性的 CircularProgressIndicator

    • indicatorColor
    • trackColor

    类似:

    <com.google.android.material.progressindicator.CircularProgressIndicator
           android:indeterminate="true"
           app:trackColor="@color/red600Light"
           app:indicatorColor="@color/red600Dark"
           app:indicatorSize="50dp"
           .../>
    

    您也可以使用多色不确定指示器。

    <com.google.android.material.progressindicator.CircularProgressIndicator
                android:indeterminate="true"
                app:trackColor="@color/red600Light"
                app:indicatorColor="@array/progress_colors"
                app:indeterminateAnimationType="contiguous"/>
    

    array/progress_colors:

      <integer-array name="progress_colors">
        <item>@color/...</item>
        <item>@color/...</item>
        <item>@color/...</item>
      </integer-array>
    

    【讨论】:

      【解决方案2】:

      在您的 AppTheme 中覆盖 android:colorControlActivated,它应该在您的 styles.xml 中:

      <style name="AppTheme" parent="...">
          ...
          <item name="android:colorControlActivated">@color/beautiful_color</item>
          ...
      </style>
      

      适用于 API 21+

      【讨论】:

      • 最简单的方法。谢谢!
      【解决方案3】:

      对于少于 23 的 API 使用

      progressBar.getIndeterminateDrawable().setColorFilter(
             getResources().getColor(Your_Color),
              android.graphics.PorterDuff.Mode.SRC_IN);
      

      别用

      progressBar.getIndeterminateDrawable().setColorFilter(
              ContextCompat.getColor(context, Your_Color),
              android.graphics.PorterDuff.Mode.SRC_IN);
      

      【讨论】:

        【解决方案4】:

        我看到其他答案已经很老了,为了更改不确定的 ProgressBar 颜色,您只需在 XML 中直接在 ProgressBar 项目中设置 android:indeterminateTintandroid:indeterminateTintMode 属性:

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateTint="@color/colorPrimary"
            android:indeterminateTintMode="src_in"
            android:indeterminate="true" />
        

        android:indeterminateTint - 应用于不确定进度指示器的色调。

        必须是颜色值,形式为"#rgb""#argb""#rrggbb""#aarrggbb",或对资源@color/colorPrimary的引用。

        android:indeterminateTintMode - 用于应用进度指示器色调的混合模式。

        必须是以下常量值之一:
        addmultiplyscreensrc_atopsrc_insrc_over

        这些属性的 Getter 和 Setter 方法是:

        全部添加到API level 21

        【讨论】:

        • 太令人沮丧了!在 android studio 中,您必须使用 View All Attributes 选项来访问 indeterminateTintMode,然后将其更改为 src_in ...我不会自己解决这个问题。谢谢!
        【解决方案5】:
        progressBar.getIndeterminateDrawable().setColorFilter(
                    getResources().getColor(Your_Color),
                    android.graphics.PorterDuff.Mode.SRC_IN);
        

        并将 Your_Color 替换为所需的颜色,例如:R.color.your_color_code

        【讨论】:

        • 这是最快捷方便的方法。
        • 可以在 xml 中定义:android:indeterminateTint="@colors/your_color"android:indeterminateTintMode="src_in"
        • @zhaoyuanjie android:indeterminateTint 仅适用于 API 级别 21+
        • 我很高兴我向下滚动 - 这应该是公认的答案。
        • 真的有必要做那些搬运工人的生意吗? :(
        【解决方案6】:
        <?xml version="1.0" encoding="UTF-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:id="@android:id/background" android:drawable="@drawable/bg" />
            <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/secondary" />
            <item android:id="@android:id/progress" android:drawable="@drawable/progress" />
        </layer-list>
        

        【讨论】:

          【解决方案7】:

          实际上你需要做的(对于圆和条)是在drawable中创建一个xml文件,如下所示...... progress_spinner_001 指向你想要动画的任何图像和持续时间......你想显示每帧多长时间...... 并设置你的 android:indeterminateDrawable= filename_in_drawable....

          <?xml version="1.0" encoding="utf-8"?>
          <animation-list
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:oneshot="false">
          <item android:drawable="@drawable/progress_spinner_001" android:duration="300" />
          <item android:drawable="@drawable/progress_spinner_002" android:duration="300" />
          <item android:drawable="@drawable/progress_spinner_003" android:duration="300" />
            <item android:drawable="@drawable/progress_spinner_004" android:duration="300" />
              <item android:drawable="@drawable/progress_spinner_005" android:duration="300" />
                <item android:drawable="@drawable/progress_spinner_006" android:duration="300" />
               <item android:drawable="@drawable/progress_spinner_007" android:duration="300" />
              <item android:drawable="@drawable/progress_spinner_008" android:duration="300" />
          </animation-list>
          

          ps 你可能需要调整进度条的大小以根据需要显示

          【讨论】:

          • -1:这个答案说“如下所示”,但它什么也没显示。我觉得很难理解。
          【解决方案8】:

          【讨论】:

            【解决方案9】:

            要在要在白色/浅色背景上使用的默认主题中获取 ProgressBar,请使用其中一种反向样式:

            <ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/>
            <ProgressBar style="@android:style/Widget.ProgressBar.Large.Inverse"/>
            <ProgressBar style="@android:style/Widget.ProgressBar.Small.Inverse"/>
            

            这通常会在透明的 ProgressBar 上显示为黑色,但某些操作系统安装使用自定义资源。如果您正在寻找特定颜色,则必须按照 CommonsWare 提供的说明滚动您自己的可绘制对象。

            【讨论】:

            • 这似乎不是与设备无关的解决方案。在我的 LG Optimus 2X Speed 上,不确定的 ProgressBar 始终为黑色,而在带有 CyanogenMod for 2.3.3 的 HTC Desire HD 上,它始终为红色。
            • @Paul - 是的,你是对的。许多设备制造商自定义主题,默认主题可以在 Android 版本之间更改。我会更正上面的文字
            猜你喜欢
            • 1970-01-01
            • 2016-01-30
            • 2010-12-01
            • 2017-11-27
            • 2012-11-01
            • 2012-11-08
            • 1970-01-01
            • 2015-11-10
            • 2011-06-08
            相关资源
            最近更新 更多