Android 更改小部件背景图片

Android Change Widget Background Image(Android 更改小部件背景图片)
本文介绍了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 更改小部件背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)