安卓更改小部件背景图片

4

过去的两天里,我一直在努力根据某些if语句(现在已经删除了,只想从类中更改widget背景)来更改widget的背景。这是我的源代码。但是,问题是我以前可以更改图像,比如背景,但是现在无法将其应用于我的widget。谢谢。顺便说一句,这是我最近的尝试。

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

2
我尝试了同样的方法但从未找到解决方案,似乎在布局充气后无法更改小部件的背景。这可能是由于RemoteView的限制。 我想要一个实心小部件(alpha=255)和一个半透明小部件(alpha=128)。作为解决方案,我创建了两个具有相应布局的布局,并在需要切换背景时切换了布局。 - theomega
不幸的是,我最终所做的事情意味着我不得不添加10个不同的XML文件,因为我只想根据不同的条件更改5个不同的图像,所以需要10个。看起来相当低效,但我们能做什么呢?我听说,冰淇淋三明治将为我们提供更好的小部件控制。 - user577732
3个回答

17

下面是一个小技巧: 用 ImageView 控件作为背景,而不是使用 View 控件的 "background" 属性,并将 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);

对我来说非常有效,非常感谢! - jamesc
@Vladimir Sizov 如何从URL加载图像? - Ganesh Kanna

2
尝试使用setImageViewResource()函数。
remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);

谢谢


我以前尝试过这种方法,因为这是对我来说最有意义的,但是按照您建议的编写代码会出现错误,即views.setImageViewResource(R.id.widget, R.drawable.widget_background);然后也无法正常工作。因此,在xml中,我将背景设置为#00000000,但是在我的类中设置的内容不显示,我只有一个透明的小部件。还有其他建议吗?谢谢。 - user577732

0

有人有其他的想法吗?真的很想解决这个问题。

编辑:最终设置了不同的布局,目前有 10 种不同的布局适用于 2 种不同的小部件,效率低下但是确实有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接