Android Change Widget Background Image

血红的双手。 提交于 2019-12-01 08:36:55

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);

Try using setImageViewResource()

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

Thanks

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!