首页 文章

如何在Android中更改进度条的进度颜色

提问于
浏览
358

我在Android应用程序中使用水平进度条,我想更改其进度颜色(默认情况下为黄色) . 我怎么能用 code (不是XML)呢?

30 回答

  • 3

    我'm sorry that it'不是答案,但是什么推动了从代码设置它的要求?如果正确定义, .setProgressDrawable 应该可以正常工作

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:id="@android:id/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="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip" />
                <gradient
                    android:startColor="@color/progress_start"
                    android:endColor="@color/progress_end"
                    android:angle="270" 
                />
            </shape>
        </clip>
    </item>
    
    </layer-list>
    
  • 3

    在修改默认进度条的外观时遇到同样的问题 . 这里有一些有希望帮助人们的信息:)

    • xml文件的名称必须只包含字符: a-z0-9_. (即没有大写字母!)

    • 要引用你的"drawable",它是 R.drawable.filename

    • 要覆盖默认外观,请使用 myProgressBar.setProgressDrawable(...) ,但是您需要不能仅将自定义布局称为 R.drawable.filename ,需要将其检索为 Drawable

    Resources res = getResources();
    myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
    
    • 您需要在设置进度/次要进度/最大值之前设置样式(之后为我设置导致'empty'进度条)
  • 21

    对于水平ProgressBar,您也可以使用 ColorFilter ,如下所示:

    progressBar.getProgressDrawable().setColorFilter(
        Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
    

    Red ProgressBar using color filter

    Note: 这会修改应用中所有进度条的外观 . 要仅修改一个特定的进度条,请执行以下操作:

    Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
    progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
    progressBar.setProgressDrawable(progressDrawable);
    

    如果progressBar不确定,则使用 getIndeterminateDrawable() 而不是 getProgressDrawable() .

    从Lollipop(API 21)开始,您可以设置进度色调:

    progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
    

    Red ProgressBar using progress tint

  • 14

    对于我的不确定进度条(微调器),我只是在drawable上设置了一个颜色过滤器 . 效果很好而且只有一条线 .

    将颜色设置为红色的示例:

    ProgressBar spinner = new android.widget.ProgressBar(
                    context,
                    null,
                    android.R.attr.progressBarStyle);
    
    spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
    

    enter image description here

  • 18

    这不是以编程方式,但我认为它可以帮助很多人!我尝试了很多,最有效的方法是将这些行添加到.xml文件中的ProgressBar中:

    android:indeterminate="true"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/secondary"
    

    所以最后这段代码为我做了:

    <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="50dp"
                android:visibility="visible"
                android:indeterminate="true"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/secondary">
    

    此解决方案适用于 API 21+

  • 4

    这是一个老问题,但这里没有提到使用主题 . 如果你的默认主题是使用 AppCompat ,你的 ProgressBar 的颜色将是你定义的 colorAccent .

    更改 colorAccent 也会更改 ProgressBar 的颜色,但是更改也会反映在多个位置 . 所以,如果你想为特定的 PregressBar 想要一种不同的颜色,你可以通过将主题应用于 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 .

    Note :使用 style 将无效 . 您只需要使用 android:theme . 你可以在这里找到更多主题:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

  • 3

    根据一些建议,您可以指定一个形状并使用颜色进行剪裁,然后进行设置 . 我以编程方式工作 . 这是我怎么做的..

    首先确保导入drawable库..

    import android.graphics.drawable.*;

    然后使用类似下面的代码;

    ProgressBar pg = (ProgressBar)row.findViewById(R.id.progress);
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
    String MyColor = "#FF00FF";
    pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    pg.setProgressDrawable(progress);   
    pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
    pg.setProgress(45);
    
  • 255

    如果不确定:

    ((ProgressBar)findViewById(R.id.progressBar))
        .getIndeterminateDrawable()
        .setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
    
  • 134

    这对我有用:

    <ProgressBar
     android:indeterminateTint="#d60909"
     ... />
    
  • 409

    如今在2016年我发现一些前Lollipop设备不符合 colorAccent 设置,因此我对所有API的最终解决方案现在如下:

    // fixes pre-Lollipop progressBar indeterminateDrawable tinting
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    
        Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
        mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
    } else {
        mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
    }
    

    对于奖励积分,它不使用任何弃用的代码 . 试试吧!

  • 6

    在这个答案中可能有一件事没有提到:

    如果您的主题继承自 Theme.AppCompat ,则 ProgressBar 将假定您在主题中定义为 "colorAccent" 的颜色 .

    所以,使用..

    <item name =“colorAccent”> @ color / custom_color </ item>

    ..将自动调整ProgressBar的颜色到 @color/custom_color .

  • 3

    我是如何在水平ProgressBar中完成的:

    LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
        Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
        progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    
  • 12

    您可以尝试更改样式,主题或使用android:indeterminateTint =“@ color / yourColor”在任何地方,但只有一种方法可以在任何Android SKD版本上使用:

    如果进度条不是不确定的,请使用:

    progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );
    

    如果进度条不确定,请使用:

    progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );
    

    Android真是太乱了!

  • 25

    这就是我所做的 . 工作 .

    ProgressBar:

    <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:indeterminateDrawable="@drawable/progressdrawable"
               />
    

    progressdrawable.xml:
    这里使用渐变来根据需要改变颜色 . 并且android:toDegrees = "X"增加X的值并且进度条快速旋转 . 减少并且旋转缓慢 . 根据您的需要定制 .

    <?xml version="1.0" encoding="utf-8"?>
         <rotate xmlns:android="http://schemas.android.com/apk/res/android"
                android:duration="4000"
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="360" >
    
                <shape
                    android:innerRadius="20dp"
                    android:shape="ring"
                    android:thickness="4dp"
                    android:useLevel="false" >
                    <size
                        android:height="48dp"
                        android:width="48dp" />
    
                    <gradient
                        android:centerColor="#80ec7e2a"
                        android:centerY="0.5"
                        android:endColor="#ffec7e2a"
                        android:startColor="#00ec7e2a"
                        android:type="sweep"
                        android:useLevel="false" />
                </shape>
    
            </rotate>
    

    sample:
    enter image description here

  • 10
    android:progressTint="#ffffff"
    
  • 24

    这个解决方案对我有用:

    <style name="Progressbar.White" parent="AppTheme">
        <item name="colorControlActivated">@color/white</item>
    </style>
    
    <ProgressBar
        android:layout_width="@dimen/d_40"
        android:layout_height="@dimen/d_40"
        android:indeterminate="true"
        android:theme="@style/Progressbar.White"/>
    
  • 4

    最简单的解决方案如果要更改布局xml文件中的颜色,请使用以下代码并使用 indeterminateTint 属性作为所需颜色 .

    <ProgressBar
          android:id="@+id/progressBar"
          style="?android:attr/progressBarStyle"
          android:layout_width="wrap_content"
          android:indeterminate="true"
          android:indeterminateTintMode="src_atop"
          android:indeterminateTint="#ddbd4e"
          android:layout_height="wrap_content"
          android:layout_marginBottom="20dp"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true" />
    
  • 3

    All API

    如果使用所有API只是在样式中创建主题

    style.xml

    <resources>
    
        //...
    
        <style name="progressBarBlue" parent="@style/Theme.AppCompat">
            <item name="colorAccent">@color/blue</item>
        </style>
    
    </resources>
    

    并在使用过程中

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

    API level 21 and higher

    如果在API级别21及更高版本中使用,只需使用此代码:

    <ProgressBar
       //...
       android:indeterminate="true"
       android:indeterminateTintMode="src_atop"
       android:indeterminateTint="@color/secondary"/>
    
  • 9

    还有一点,主题解决方案确实有效,如果你继承了一个基本主题,那么对于app compact,你的主题应该是:

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

    然后在进度条主题中设置它

    <ProgressBar
        android:id="@+id/progressCircle_progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:theme="@style/AppTheme.Custom"
        android:indeterminate="true"/>
    
  • 11

    对于水平样式ProgressBar,我使用:

    import android.widget.ProgressBar;
    import android.graphics.drawable.GradientDrawable;
    import android.graphics.drawable.ClipDrawable;
    import android.view.Gravity;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.LayerDrawable;
    
    public void setColours(ProgressBar progressBar,
                            int bgCol1, int bgCol2, 
                            int fg1Col1, int fg1Col2, int value1,
                            int fg2Col1, int fg2Col2, int value2)
      {
        //If solid colours are required for an element, then set
        //that elements Col1 param s the same as its Col2 param
        //(eg fg1Col1 == fg1Col2).
    
        //fgGradDirection and/or bgGradDirection could be parameters
        //if you require other gradient directions eg LEFT_RIGHT.
    
        GradientDrawable.Orientation fgGradDirection
            = GradientDrawable.Orientation.TOP_BOTTOM;
        GradientDrawable.Orientation bgGradDirection
            = GradientDrawable.Orientation.TOP_BOTTOM;
    
        //Background
        GradientDrawable bgGradDrawable = new GradientDrawable(
                bgGradDirection, new int[]{bgCol1, bgCol2});
        bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
        bgGradDrawable.setCornerRadius(5);
        ClipDrawable bgclip = new ClipDrawable(
                bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
        bgclip.setLevel(10000);
    
        //SecondaryProgress
        GradientDrawable fg2GradDrawable = new GradientDrawable(
                fgGradDirection, new int[]{fg2Col1, fg2Col2});
        fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
        fg2GradDrawable.setCornerRadius(5);
        ClipDrawable fg2clip = new ClipDrawable(
                fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        
    
        //Progress
        GradientDrawable fg1GradDrawable = new GradientDrawable(
                fgGradDirection, new int[]{fg1Col1, fg1Col2});
        fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
        fg1GradDrawable.setCornerRadius(5);
        ClipDrawable fg1clip = new ClipDrawable(
                fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        
    
        //Setup LayerDrawable and assign to progressBar
        Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
        LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
        progressLayerDrawable.setId(0, android.R.id.background);
        progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
        progressLayerDrawable.setId(2, android.R.id.progress);
    
        //Copy the existing ProgressDrawable bounds to the new one.
        Rect bounds = progressBar.getProgressDrawable().getBounds();
        progressBar.setProgressDrawable(progressLayerDrawable);     
        progressBar.getProgressDrawable().setBounds(bounds);
    
        // setProgress() ignores a change to the same value, so:
        if (value1 == 0)
            progressBar.setProgress(1);
        else
            progressBar.setProgress(0);
        progressBar.setProgress(value1);
    
        // setSecondaryProgress() ignores a change to the same value, so:
        if (value2 == 0)
            progressBar.setSecondaryProgress(1);
        else
            progressBar.setSecondaryProgress(0);
        progressBar.setSecondaryProgress(value2);
    
        //now force a redraw
        progressBar.invalidate();
      }
    

    一个示例调用将是:

    setColours(myProgressBar, 
              0xff303030,   //bgCol1  grey 
              0xff909090,   //bgCol2  lighter grey 
              0xff0000FF,   //fg1Col1 blue 
              0xffFFFFFF,   //fg1Col2 white
              50,           //value1
              0xffFF0000,   //fg2Col1 red 
              0xffFFFFFF,   //fg2Col2 white
              75);          //value2
    

    如果您不需要“二次进度”,只需将value2设置为value1 .

  • 211

    使用android.support.v4.graphics.drawable.DrawableCompat

    Drawable progressDrawable = progressBar.getIndeterminateDrawable();
                if (progressDrawable  != null) {
                    Drawable mutateDrawable = progressDrawable.mutate();
                    DrawableCompat.setTint(mutateDrawable, primaryColor);
                    progressBar.setProgressDrawable(mutateDrawable);
                }
    
  • 2

    发布添加关于PaulieG's answer的信息,因为ateiob让我解释一下......


    我可以说 ProgressBar 代码中的错误/问题/优化忽略了将进度设置为值的尝试(或者至少在编写本文时,我查看了当前版本的Android源代码)它已经在了 .

    • ,即如果progress = 45,并且您尝试将其设置为45,则代码将不执行任何操作,并且 will not redraw the progress .

    调用 ProgressBar.setProgressDrawable() 后,您的进度条将为空(因为您更改了可绘制部分) .

    这意味着您需要设置进度并重新绘制 . 但是,如果您只是将进度设置为保留值,它将不执行任何操作 .

    您必须先将其设置为0,然后再将其设置为“旧”值,并且该栏将重绘 .


    总结一下:

    • 保留"old"进度值

    • 更新drawable / color(使条形空白)

    • 将进度重置为0(否则下一行不执行任何操作)

    • 将进度重置为"old"值(修复栏)

    • 无效


    以下是我的方法:

    protected void onResume()
    {
        super.onResume();
        progBar = (ProgressBar) findViewById(R.id.progress_base);
    
        int oldProgress = progBar.getProgress();
    
        // define new drawable/colour
        final float[] roundedCorners = new float[]
            { 5, 5, 5, 5, 5, 5, 5, 5 };
        ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
            roundedCorners, null, null));
        String MyColor = "#FF00FF";
        shape.getPaint().setColor(Color.parseColor(MyColor));
        ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
            ClipDrawable.HORIZONTAL);
        progBar.setProgressDrawable(clip);
    
        progBar.setBackgroundDrawable(getResources().getDrawable(
            android.R.drawable.progress_horizontal));
    
        // work around: setProgress() ignores a change to the same value
        progBar.setProgress(0);
        progBar.setProgress(oldProgress);
    
        progBar.invalidate();
    }
    

    至于HappyEngineer的解决方案,我认为这是一个类似的解决方法,手动设置“进度”偏移量 . 无论哪种情况,上述代码都适合您 .

  • 34
    ProgressBar bar;
    
    private Handler progressBarHandler = new Handler();
    
    GradientDrawable progressGradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
                    0xff1e90ff,0xff006ab6,0xff367ba8});
    ClipDrawable progressClipDrawable = new ClipDrawable(
            progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    Drawable[] progressDrawables = {
            new ColorDrawable(0xffffffff),
            progressClipDrawable, progressClipDrawable};
    LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
    
    
    int status = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // TODO Auto-generated method stub
        setContentView(R.layout.startup);
    
        bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
        bar.setProgress(0);
        bar.setMax(100);
    
        progressLayerDrawable.setId(0, android.R.id.background);
        progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
        progressLayerDrawable.setId(2, android.R.id.progress);
    
        bar.setProgressDrawable(progressLayerDrawable);
    }
    

    这有助于我通过代码为进度条设置自定义颜色 . 希望能帮助到你

  • 11

    将此自定义样式应用于进度栏 .

    <style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
            <item name="android:indeterminateDrawable">@drawable/progress</item>
            <item name="android:duration">40</item>
            <item name="android:animationCache">true</item>
            <item name="android:drawingCacheQuality">low</item>
            <item name="android:persistentDrawingCache">animation</item>
        </style>
    

    @ drawable / progress.xml -

    <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/spinner_white"
        android:pivotX="50%"
        android:pivotY="50%" />
    

    将此类图像用于进度条 .
    enter image description here

    为获得更好的效果,您可以使用多个进度图像请不要犹豫使用图像,因为Android平台本身使用图像进行处理 . 代码是从sdk中提取的:)

  • 4

    根据muhammad-adil建议使用SDK ver 21及更高版本

    android:indeterminateTint="@color/orange"
    

    在XML中为我工作,很容易 .

  • 12

    使用attr非常简单,如果你正在处理multistyle应用程序:

    试试这种方式:

    在属性attrs.xml下面声明

    <attr name="circularProgressTheme" format="reference"></attr>
    

    粘贴到styles.xml中的代码下面

    <style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
            <item name="colorAccent">#FF0000</item>
        </style>
    
        <style name="circularProgressThemeWhite">
            <item name="android:theme">@style/ProgressThemeWhite</item>
        </style>
    
    
      <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    
       <item name="circularProgressTheme">@style/circularProgressThemeWhite</item>
    
     </style>
    

    使用下面的进度条

    <ProgressBar
            style="?attr/circularProgressTheme"
            android:id="@+id/commonProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="visible"/>
    
  • 12

    以下是以编程方式更改ProgressBar颜色的代码 .

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

    Horizontal ProgressBar 使用 rectangle shape drawable作为背景, ClipDrawable 使用 rectangle shape 构建进度(主要和次要) . 着色将颜色更改为某些色调 . 分别为 If you want targeted colors 所有三个,您可以使用 ProgressBar.setProgressDrawable() 如下:

    LayerDrawable progressBarDrawable = new LayerDrawable(
            new Drawable[]{
                    new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),
    
                    new ClipDrawable(
                                new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                        new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                                Gravity.START,
                                ClipDrawable.HORIZONTAL),
    
                    new ClipDrawable(
                            new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                    new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                            Gravity.START,
                            ClipDrawable.HORIZONTAL)
                });
    
        progressBarDrawable.setId(0,android.R.id.background);
        progressBarDrawable.setId(1,android.R.id.secondaryProgress);
        progressBarDrawable.setId(2,android.R.id.progress);
        ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);
    

    请注意,初始化 LayerDrawable 时,可绘制层的顺序很重要 . 第一个drawable应该是背景 . 根据我的实验,切换ID不起作用 . 如果将填充设置为 progressbar ,则此方法将不起作用 . 如果需要填充,则可以使用容器作为进度条,例如 LinearLayout .

  • 2

    Horizontal progress bar custom material style :

    要更改背景颜色和水平进度条的进度 .

    <style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressBackgroundTint">#69f0ae</item>
        <item name="android:progressTint">#b71c1c</item>
        <item name="android:minWidth">200dp</item>
    </style>
    

    通过设置样式属性将其应用于进度条,对于自定义材质样式和自定义进度条检查http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples

  • 211
    ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);
    
    freeRamPb.getProgressDrawable().setColorFilter(
    Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
    

相关问题