我在我的Android应用程序中使用水平进度条,我想改变它的进度颜色(默认为黄色)。我如何使用代码(而不是XML)做到这一点?
当前回答
ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);
freeRamPb.getProgressDrawable().setColorFilter(
Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
其他回答
很抱歉,这不是答案,但是是什么驱使需求从代码中设置它呢? 如果定义正确。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>
对于水平风格的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。
根据一些建议,你可以指定一个形状和可剪切的颜色,然后设置它。我让它以编程的方式工作。我就是这么做的。
首先确保导入了可绘制库。
进口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);
简单的使用方法:
PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
mode = PorterDuff.Mode.MULTIPLY;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(Color.RED));
} else {
Drawable progressDrawable;
progressDrawable = (progressBar.isIndeterminate() ? progressBar.getIndeterminateDrawable() : progressBar.getProgressDrawable()).mutate();
progressDrawable.setColorFilter(context.getResources().getColor(Color.RED), mode);
progressBar.setProgressDrawable(progressDrawable);
}
所有接口
如果使用所有的API只创建主题的样式
style.xml
<resources>
//...
<style name="progressBarBlue" parent="@style/Theme.AppCompat">
<item name="colorAccent">@color/blue</item>
</style>
</resources>
在进行中使用
<ProgressBar
...
android:theme="@style/progressBarBlue" />
空气污染指数21或以上
如果在API级别21及以上使用,请使用以下代码:
<ProgressBar
//...
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/secondary"/>
推荐文章
- 如何在Android中制作倒计时计时器?
- Android中的。dex文件是什么?
- 如何将一些数据传输到另一个片段?
- 在Android操作系统上运行Haskell程序
- Android上的FFmpeg
- 横向列表视图在Android?
- 标准的Android菜单图标,例如刷新
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?