[英]Android Change Widget Background Image
過去兩天一直在努力改變我的小部件的背景,基於一些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>
這是一個你可以做的技巧:使用ImageView為你的背景,而不是你的“背景”屬性查看並將scaleType設置為“fitXY”。 像這樣:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/some_background"
android:scaleType="fitXY"/>
<RelativeLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="some button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- all your views -->
</RelativeLayout>
</FrameLayout>
現在,您可以在運行時切換ImageView源:
//updating current widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_layout);
views.setImageViewResource(R.id.backgroundImage, R.drawable.some_other_background);
appWidgetManager.updateAppWidget(widgetId, views);
嘗試使用setImageViewResource()
remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);
謝謝
有人有任何其他想法嗎? 真的很想弄清楚這一點
編輯:最終設置不同的布局目前有10個不同的布局,2個不同的小部件效率低下,但真正工作更多的黑客真的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.