android开发分享Android更改Horizo​​nal Progress bar颜色

我已经设置了水平进度条。

我想改变黄色的进度颜色。

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

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

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

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

    然后创build一个类似于以下内容的新drawable(在本例中为greenprogress.xml):

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="https://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="#33FF33" android:endColor="#008000" android:angle="270" /> </shape> </clip> </item> </layer-list> 

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

    更简单的解决scheme:

    progess_drawable_blue

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="https://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> 

    如果您只想更改进度栏颜色,则可以在Activity的onCreate()方法中简单地使用颜色filter:

     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); 

    想法从这里 。

    你只需要做这个前棒棒糖版本。 在棒棒糖上,你可以使用colorAccent风格属性。

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

    特别是#2和#3有很多混乱,正如在amfcosta的回答中所看到的那样。 这个答案会产生不可预知的颜色结果,只要你想将进度条设置为除原色以外的任何东西,因为它只会修改背景颜色,而实际的进度条“剪辑”区域仍然是一个黄色覆盖层,不透明度降低。 例如,使用该方法将背景设置为深紫色将导致由暗紫色和黄色混合通过缩小的阿尔法产生的某种疯狂粉红色的进度条“剪辑”颜色。

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


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

    res / drawable / my_progressbar.xml

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

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

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="https://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) 

    如何以编程方式调整进度栏颜色,并以编程方式创build颜色

    编辑:我发现时间正确地解决这个问题。 我以前的回答离开了这个有点模糊。

    ProgressBar在LayerDrawable中组成3个Drawable。

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

     //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); 

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

     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); 

    干杯。

    -槊

    对于任何寻找如何编程的人:

      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); 

    设置可绘制的ID是至关重要的,并保留进度条的边界和​​实际状态

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

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

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

      <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:theme="@style/hProgressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminateOnly="true" /> 

    就这样。

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

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

    把上面的文件放在res > values文件夹下的styles.xml文件中。

    注:如果您使用任何其他口音的颜色,那么以前的解决scheme应该是很好的去。

    API 21或更高

    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> 

    的onCreate:

     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); } 

    对于API级别21或更高的用途

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

    最终LayerDrawable图层=(LayerDrawable)progressBar.getProgressDrawable(); layers.getDrawable(2).setColorFilter(颜色,PorterDuff.Mode.SRC_IN);

    你们真的让我很头疼 你可以做的是使你的图层列表首先通过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文件屏幕文件夹,那么只需修改颜色码。 不pipe是从头开始重新实例化一个新的ShapeDrawable。

    上面的答案改变了整个背景颜色,它的ambigios它将进度条的高度改变到最大值,而不能在xml中改变。 改变进度条状态的更好方法是完成20%的红色,50%的黄色,70%以上的绿色。 你可以用java编程。

    如果您有其他解决scheme,请分享您的答案。

      以上就是android开发分享Android更改Horizo​​nal Progress bar颜色相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/516800.html

      (0)
      上一篇 2020年12月3日
      下一篇 2020年12月3日

      精彩推荐