首页 > 解决方案 > 如何以编程方式更改主屏幕小部件的形状颜色

问题描述

我希望我的主屏幕小部件是一个带角的矩形,所以我有一个可绘制的形状。然而,纯色需要在按下按钮时更改以匹配我的文本背景颜色。我可以通过这种方式更改我的文本背景颜色:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

backgroundColor = colors[mCounter];
views.setInt(R.id.appwidget_text, "setBackgroundColor", backgroundColor);

我尝试了以下但没有运气:

views.setInt(R.drawable.layout_bg, "setBackgroundColor", backgroundColor);

我还尝试更改我的小部件的 backgroundTint,以及以下内容:

views.setInt(R.id.appwidget_layout, "setBackgroundTint", backgroundColor);

这也没有任何作用。

有解决办法吗?

标签: javaandroidandroid-widget

解决方案


int[] color = new int[2];
color[0] = Color.parseColor("#000000");
color[1] = Color.parseColor("#000000");

View view = new RelativeLayout(this);
GradientDrawable drawable = (GradientDrawable) view.getBackground();

drawable.setColors(color);

尝试这个。对于你的问题,

int[] color = new int[2];
color[0] = Color.parseColor("#000000");
color[1] = Color.parseColor("#000000");
GradientDrawable drawable = (GradientDrawable) views.getBackground();
drawable.setColors(color);

希望它会有所帮助。:)


推荐阅读