Android Change Widget Background Image

2019-02-18 23:10发布

Been struggling for the past two days to change the background of my widget, based on some if statements (removed right now just want to change the widget background from the class) here is my source below. What's up though, I've changed images before fine such as backgrounds but can not get it to work for my widget thank you. This is my most recent attempt by the way

//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>

3条回答
对你真心纯属浪费
2楼-- · 2019-02-18 23:32

Anyone got any other ideas? Really would like to get this figured out

Edit: ended up setting up different layouts currently have 10 different layouts for 2 different widgets inefficient but working more of a hack around really

查看更多
甜甜的少女心
3楼-- · 2019-02-18 23:40

Here's a trick that you can do: use ImageView for you background, not "background" property of you View and set scaleType to "fitXY". Like this:

<?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>

Now you can switch your ImageView source during runtime:

//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);
查看更多
男人必须洒脱
4楼-- · 2019-02-18 23:51

Try using setImageViewResource()

remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);

Thanks

查看更多
登录 后发表回答