2016-02-17 34 views
10

我的插件的佈局文件開始在頂層有:設置AppWidget背景一個小部件影響其他太

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/widget_margin" 
    android:id="@+id/widget" 
    android:background="#20000000" > 

所以,起始背景是半透明的。然後,在執行過程中的某個時刻,我呼籲以下,以使背景完全透明:

remoteViews.setInt(R.id.widget, "setBackgroundColor", Color.parseColor("#00000000")); 
appWidgetManager.updateAppWidget(appWidgetId, remoteViews); 

雖然這個作品,就是我的發現是,在添加其他小部件,該插件的背景開始起飛根據佈局xml完全透明而不是半透明。就好像爲一個小部件設置背景顏色已經改變了所有的部分,包括隨後添加的新小部件。

現在,這是奇怪的,因爲在同一時間,上面的代碼中,我還添加了位圖到小工具(通過RemoteViews)有:

remoteViews.setImageViewBitmap(R.id.imageView, bmp); 

...和位圖沒有按將不會出現在所有後續的小部件中,因此我使用的基本方法似乎很有用。

那麼爲什麼背景傳播到其他小部件?

EDIT添加的appWidgetId推導作爲評論請求:

基本上來自一個Intent,例如在該插件的配置ActivityonCreate()

Intent intent = getIntent(); 
Bundle extras = intent.getExtras(); 
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
+0

你能向我們展示如何創建這個小部件,以及如何添加新的元素? – csenga

+1

你在哪裏添加'remoteViews.setImageViewBitmap(R.id.imageView,bmp);'? –

回答

0

在你AppWidgetProvider類重寫onUpdate方法。最後一個參數是appWidgetIds,它是當前活動小部件ID的數組。很可能你實現了在每個widgetId中循環遍歷該數組的背景顏色。爲了能夠爲每個小部件定義一個自定義背景,您需要實現將所需背景映射到特定widgetId的邏輯,並且尊重該循環內部的邏輯。