简体   繁体   中英

Changing background of a button in widget: Android

I need to change background of a button programmatically. That button is placed in a widget.

I got an example of changing background color here , but this is only for changing color, and I want to use an image instead of a color.

Is there any way to apply a background image to a button programmatically?

Thank you

不用像您链接的示例那样使用setBackgroundColor ,您应该可以使用我认为的setBackgroundResource

In Your AppWidgetProvider class:

private RemoteViews v;
... in onUpdate:
v = new RemoteViews(context.getPackageName(), R.layout.widget);
...
icon = context.getResources().getIdentifier("ic_action_refresh_"+choosenTheme, "drawable", context.getPackageName());
v.setInt(R.id.wi_bu_refresh, "setBackgroundResource", icon);

Where:

icon is just resource id (found by getIdentifier method dynamically for every theme).

R.layout.widget is widget xml layout.

R.id.wi_bu_refresh is a button in widget layout, which background You want to change.

Hope it helps, cheers.

您可以使用此:

setInt(YouButtonId, "setBackgroundResource", YourImageResourceID); 

使用setBackgroundResource而不是setBackgroundColor

我认为您需要使用public void setBackgroundResource (int resid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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