【发布时间】:2011-10-30 08:03:06
【问题描述】:
过去两天一直在努力更改我的小部件的背景,基于一些 if 语句(现在删除只是想从类中更改小部件背景)这里是我的源代码如下。不过怎么回事,我之前已经更改了图像,例如背景,但无法让它为我的小部件工作,谢谢。顺便说一下,这是我最近的尝试
//Widget Provider Class
public class WidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, com.widget.WidgetDialog.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
views.setImageViewBitmap(R.id.widget, ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background)).getBitmap());
}
}
}
//Widget Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/widget"
android:background="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</Button>
</LinearLayout>
【问题讨论】:
-
我尝试了同样的方法,但从未找到解决方案,似乎在布局膨胀后您无法更改小部件的背景。这可能是由于 RemoteView-Limitations。我想要一个实心小部件(alpha=255)和一个半透明的(alpha=128)。作为一种解决方案,我创建了两个具有相应布局的布局,并在我想切换背景时切换了这些布局。
-
不幸的是我最终做的事情意味着我必须添加 10 个不同的 xml,因为我只是想根据不同的 if 更改 5 个不同的图像,所以需要 10 个似乎效率很低,但你能做什么我听到不过冰淇淋三明治会给我们更好的小部件控制
标签: android background bitmap widget drawable