2011-06-13 98 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);

畢竟,佈局並不是那麼昂貴。