有没有办法在运行时更改应用程序小部件中ProgressBar
的颜色?
下面是ProgressBar
进度值的更新方式:
RemoteViews views = new RemoteViews( context.getPackageName(), R.layout.widget );
views.setProgressBar( R.id.progressbar, 100, 10, false );
...
appWidgetManager.updateAppWidget( widgetID, views );
不幸的是,RemoteViews
类不允许我们为ProgressBar
设置滤色器,甚至不允许绘制不同的进度。
任何想法都将不胜感激。
发布于 2018-12-26 17:58:19
我在自定义通知布局中遇到过类似的tint ProgressBar问题。我已经设法使用反射API做到了这一点:
Method setTintMethod = null;
try {
setTintMethod = RemoteViews.class.getMethod("setProgressTintList", int.class, ColorStateList.class);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (setTintMethod != null) {
try {
setTintMethod.invoke(mYourRemoteViews, R.id.your_progress_bar_id, ColorStateList.valueOf(yourColor));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
发布于 2015-01-25 21:40:26
在ProgressBar XML中添加以下行:
android:progressDrawable="@drawable/progressbar"
现在创建progressbar.xml可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
发布于 2019-10-29 20:33:14
例如,您的progressDrawable (layer-list)具有:
你可以使用这个技巧:
<代码>G215
https://stackoverflow.com/questions/28136963
复制相似问题