简体   繁体   English

如何以编程方式更改主屏幕小部件的形状颜色

[英]How to change shape color programmatically for a homescreen widget

I want my homescreen widget to be a rectangle with corners so I have a shape drawable.我希望我的主屏幕小部件是一个带角的矩形,所以我有一个可绘制的形状。 The solid color, however, needs to change on the press of a button to match my text background color.但是,纯色需要在按下按钮时更改以匹配我的文本背景颜色。 I am able to change my text background color in this way:我可以通过这种方式更改我的文本背景颜色:

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

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

I tried the following but no luck:我尝试了以下但没有运气:

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

I have also tried changing the backgroundTint of my widget, and the following:我还尝试更改我的小部件的 backgroundTint,以及以下内容:

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

This does not do anything either.这也没有任何作用。

Is there a solution?有解决办法吗?

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

try this.尝试这个。 for your problem,对于你的问题,

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

hope it will helps.希望它会有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM