2011-06-13 97 views
21

我有一些remoteView与ImageViews,我需要以编程方式更改“android:background”。更改remoteView ImageView背景

我知道如何更改“机器人:SRC”有:

remoteView.setImageViewResource(int viewId, int srcId); 

,它工作正常,但我要如何改变“机器人:背景”?

谢谢

回答

68

您可以使用public void setInt (int viewId, String methodName, int value)方法。

remoteView.setInt(R.id.viewid, "setBackgroundResource", R.color.your_color) 
+5

Only only from 2.2 ... – BrainCrash 2011-06-17 13:20:41

+1

@jamapag这不适用于Android 4.03。 – herbertD 2013-01-04 08:41:43

+0

这是在Android SDK SDK版本7中使用SDK 7的工作否@RemotableViewMethod注释[source here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1 _r2/android/view/View.java#View.setBackgroundResource%28int%29),并在SDK 8中添加了[source here](http://grepcode.com/file/repository.grepcode.com/java/ext/com .google.android/android/2.2_r1.1/android/view/View.java#View.setBackgroundResource%28int%29),这个注解只是setBackgroundResource()方法不起作用的一个原因。 – 2013-01-16 08:42:04

0

使用setBackgroundResource(int)

此外,您可能会注意到这一点。当您查看Android documentation时,如果有一个可更改的View的xml元素,它通常指向在运行时进行更改的方法。

+1

我知道使用setBackgroundResource(int),但不幸的是它不能在remoteViews上使用。 – BrainCrash 2011-06-13 19:25:06

+1

该问题未能提及您正在使用RemoveViews对象。请更具体一些。 – 2011-06-13 19:31:08

+2

标题和描述中出现“小部件”一词,该示例也以“remoteView”开头。无论如何,我会编辑它以使其更清楚。 – BrainCrash 2011-06-16 11:02:05

0

的杰里 - 钻机的方式

我想你可以用杰里 - 钻机的方式在进行第二次布局,新的背景,那么您将使用这种新的布局,这样您的远程视窗做到这一点:

  • RemoteViews remoteViews =新RemoteViews(context.getPackageName(),R.layout WIDGET1);
  • RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout。widget2);

毕竟,布局并不是那么昂贵。