Android 更改小部件背景图片

2023-09-09移动开发问题
14

本文介绍了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 更改小部件背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Cocos2d 播放2种不同的背景音乐文件或循环播放效果
Cocos2d play 2 different background music files or loop playEffect(Cocos2d 播放2种不同的背景音乐文件或循环播放效果)...
2024-08-12 移动开发问题
30

观看 Game Center 屏幕(排行榜、成就)时,如果在 iOS7 中后台运行 cocos2d 2.1 应用程序会崩
Crash if backgrounding cocos2d 2.1 app in iOS7 while watching Game Center screens (leaderboard, achievement)(观看 Game Center 屏幕(排行榜、成就)时,如果在 iOS7 中后台运行 cocos2d 2.1 应用程序会崩溃)...
2024-08-12 移动开发问题
2

Cocos2d 字体:如何为 CCLabelBMFont 创建 .fnt 文件?(任何免费的位图字体创建工具?)
Cocos2d font : How to create .fnt file for CCLabelBMFont? ( Any free bitmap font creation tool ? )(Cocos2d 字体:如何为 CCLabelBMFont 创建 .fnt 文件?(任何免费的位图字体创建工具?))...
2024-08-12 移动开发问题
2

cocos2d如何快速绘制背景?
How to draw a background fast in cocos2d?(cocos2d如何快速绘制背景?)...
2024-08-12 移动开发问题
5

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d中如何在CCLabelTTF中设置背景色
How to set background color in CCLabelTTF in cocos2d(cocos2d中如何在CCLabelTTF中设置背景色)...
2024-08-12 移动开发问题
5