gpt4 book ai didi

android - 如何将 setBackground 与 Android 小部件一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:58 26 4
gpt4 key购买 nike

我之前问过类似的问题,但是我的要求太含糊了。下面的代码绘制了一个漂亮的程式化按钮。当你点击按钮时,你可以输入一个数字,并根据数字,使用

改变背景颜色

remoteViews.setInt(R.id.nmcButton, "setBackgroundColor", color);

不幸的是,当我尝试使用

来保留样式时

remoteViews.setInt(R.id.nmcButton, "setBackground", color);

小部件不会加载。有没有解决的办法?有没有办法在更改背景颜色的同时保留样式?

这里是一些相关的文件

    package test.widget;

import android.os.Bundle;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.graphics.Color;

public class MyWidget extends AppWidgetProvider {

private final static String TEST_ACTIVITY = "test.widget.action.TEST_ACTIVITY";
private final static int INTENT_NO_REQUEST = 0; /* no requestCode */
private final static int INTENT_NO_FLAGS = 0; /* code for no Flags */
private int count = 9;

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Bundle b = intent.getExtras();
if (b != null) {
count = b.getInt("nmcCount");
callOnUpdate(context);
}
}

private void callOnUpdate(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
ComponentName thisAppWidget = new ComponentName(
context.getPackageName(), MyWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
buildUpdate(context, appWidgetManager, appWidgetIds);
}

private void buildUpdate(Context context,
AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);

Intent intent = new Intent(TEST_ACTIVITY);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
INTENT_NO_REQUEST, intent, INTENT_NO_FLAGS);
remoteViews.setOnClickPendingIntent(R.id.nmcButton, pendingIntent);
remoteViews.setTextViewText(R.id.nmcButton, String.valueOf(count));

// the code below works, but the button does not have nice styling
// int color = (count >= 5) ? Color.GREEN : Color.RED;
// remoteViews.setInt(R.id.nmcButton, "setBackgroundColor", color);

// this code doesn't work, you get "problem loading widget"
int color = (count >= 5) ? R.drawable.btn_green
: R.drawable.btn_red;
remoteViews.setInt(R.id.nmcButton, "setBackground", color);

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}

btn_red.xml

    <?xml version="2.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"><shape>
<solid android:color="#ef4444" />

<stroke android:width="1dp" android:color="#992f2f" />

<corners android:radius="3dp" />

<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape></item>
<item><shape>
<gradient android:angle="270" android:endColor="#992f2f" android:startColor="#ef4444" />

<stroke android:width="1dp" android:color="#992f2f" />

<corners android:radius="3dp" />

<padding android:bottom="11dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape></item>

</selector>

样式.xml

    <resources>
<style name="ButtonText">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">1dp</item>
<item name="android:textSize">10dp</item>
<item name="android:textStyle">normal</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
<item name="android:textColor">#ffffff</item>
</style>
<style name="AppTheme" parent="android:Theme.Light" />
</resources>

最佳答案

一位同事给出了答案,我应该使用:

remoteViews.setInt(R.id.nmcButton, "setBackgroundResource", color);

代替:

remoteViews.setInt(R.id.nmcButton, "setBackground", color);

关于android - 如何将 setBackground 与 Android 小部件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com