如何更改小部件中的ProgressBar颜色? [英] How to change the ProgressBar color in a widget?

查看:82
本文介绍了如何更改小部件中的ProgressBar颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以在运行时更改驻留在应用程序小部件中的ProgressBar的颜色?

Is there any way to change the color of the ProgressBar residing in the app widget at runtime?

这是更新ProgressBar进度值的方式:

This is how the ProgressBar progress value is updated:

RemoteViews views = new RemoteViews( context.getPackageName(), R.layout.widget );
views.setProgressBar( R.id.progressbar, 100, 10, false );
...
appWidgetManager.updateAppWidget( widgetID, views );

不幸的是,RemoteViews类不允许我们为ProgressBar设置颜色滤镜或其他可绘制的进度.

Unfortunately, the RemoteViews class does not allow us to set a color filter or even a different progress drawable for ProgressBar.

任何想法都会受到赞赏.

Any ideas would be appreciated.

推荐答案

我在自定义通知布局内将ProgressBar着色时遇到了类似的问题. 我已经使用反射API做到了这一点:

I have encountered with similar problem to tint ProgressBar inside custom notification layout. I have managed to do it using reflection 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();
        }
    }

这篇关于如何更改小部件中的ProgressBar颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆