【问题标题】:How to change shape color programmatically for a homescreen widget如何以编程方式更改主屏幕小部件的形状颜色
【发布时间】:2020-03-06 09:09:40
【问题描述】:

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

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

这也不做任何事情。

有解决办法吗?

【问题讨论】:

  • 您找到解决方法了吗?我还需要一个可以为我的小部件动态更改颜色的圆形背景。
  • 不,最后我用直角制作了我的小部件,并放弃了尝试给它们一个圆形的形状......从 Android 12 开始,无论如何它们都会自动被系统四舍五入。但当然不是每个人都在 12 岁。

标签: java android android-widget


【解决方案1】:
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);

希望它会有所帮助。 :)

【讨论】:

  • 谢谢,但 getBackground() 不是视图的选项(如远程视图)。
  • 我看到了兄弟。我也试试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
相关资源
最近更新 更多