के साथ सेटबैकग्राउंड का उपयोग कैसे करें मैंने पहले एक समान प्रश्न पूछा लेकिन मैं अपने अनुरोध में बहुत अस्पष्ट था। नीचे दिया गया कोड एक अच्छा स्टाइलिज्ड बटन खींचता है। जब आप बटन पर क्लिक करते हैं, तो आप संख्या को इनपुट कर सकते हैं और संख्या के आधार पर,एंड्रॉइड विजेट
रिमोट व्यू.सेटआईएनटी (आर.आईडी.एनएमसी बटन, "सेटबैकग्राउंड कॉलर", रंग) का उपयोग करके पृष्ठभूमि रंग बदल सकते हैं;
दुर्भाग्य, जब मैं
remoteViews.setInt (R.id.nmcButton, "setBackground", रंग) का उपयोग करके स्टाइल को संरक्षित करने की कोशिश;
विजेट लोड नहीं होगा। क्या इसके चारों ओर एक रास्ता है? पृष्ठभूमि रंग बदलने के दौरान स्टाइल को संरक्षित करने का कोई तरीका है?
यहाँ प्रासंगिक फ़ाइलें
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>
styles.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>
काम नहीं करता है। स्टाइल हटा दिया गया है। यह आपके अपने प्रश्न का उत्तर नहीं देता – AxeManTOBO