Android 更改小部件背景图片 [英] Android Change Widget Background Image

查看:31
本文介绍了Android 更改小部件背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去两天一直在努力更改我的小部件的背景,基于一些 if 语句(现在删除只是想从类中更改小部件背景)这里是我下面的来源.不过,我之前已经更改了图片,例如背景,但无法让它为我的小部件工作,谢谢.顺便说一下,这是我最近的尝试

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>

推荐答案

有人有其他想法吗?真的很想弄清楚这一点

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

最终设置了不同的布局,目前有 10 种不同的布局用于 2 个不同的小部件效率低下,但实际上工作更多的是 hack

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

这篇关于Android 更改小部件背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆