2012-11-21 20 views
5

के साथ सेटबैकग्राउंड का उपयोग कैसे करें मैंने पहले एक समान प्रश्न पूछा लेकिन मैं अपने अनुरोध में बहुत अस्पष्ट था। नीचे दिया गया कोड एक अच्छा स्टाइलिज्ड बटन खींचता है। जब आप बटन पर क्लिक करते हैं, तो आप संख्या को इनपुट कर सकते हैं और संख्या के आधार पर,एंड्रॉइड विजेट

रिमोट व्यू.सेटआईएनटी (आर.आईडी.एनएमसी बटन, "सेटबैकग्राउंड कॉलर", रंग) का उपयोग करके पृष्ठभूमि रंग बदल सकते हैं;

दुर्भाग्य

, जब मैं

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> 

उत्तर

13

एक सह कार्यकर्ता जवाब था, मैं का इस्तेमाल किया है चाहिए :

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

बजाय:

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

काम नहीं करता है। स्टाइल हटा दिया गया है। यह आपके अपने प्रश्न का उत्तर नहीं देता – AxeManTOBO

1

मैं इस लेकिन वह करने की कोशिश की मेरे लिए काम नहीं किया।

आप अपने विजेट में एक रंग आप इस करना चाहिए सेट करना चाहते हैं:

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

मैं डाउनवॉटेड क्योंकि यह सेटबैकग्राउंड कॉलर –

+0

@ ब्रेनो सल्गाडो के बाद खोने वाली शैली के मुद्दे को संबोधित नहीं करता है फिर डाउनवोट क्यों? यह आपके लिए जवाब नहीं हो सकता है, लेकिन यह निश्चित रूप से मेरे लिए पूरी तरह से काम करता है।उपर्युक्त उत्तर भी उस मुद्दे को सही नहीं संबोधित करता है? आप इस टिप्पणी का उपयोग करने के बाद भी अपनी टिप्पणी खो सकते हैं कि आप एक टिप्पणी छोड़ सकते हैं। –

+0

कुछ भी निजी नहीं है लेकिन मुझे लगता है कि यह प्रश्न को सही तरीके से संबोधित नहीं करता है। मुझे लगता है कि जावा/एंड्रॉइड उत्तर की गुणवत्ता कुछ हद तक कम है क्योंकि इसमें बहुत कुछ है। रूबी के जवाब में (उदाहरण के तौर पर) मुझे यह समझाने की कोशिश करने के लिए इस्तेमाल किया गया था कि पाठक को क्या समस्या है, समाधान का सुझाव देना, ओपी में किए गए प्रत्येक प्रश्न को संबोधित करना, लिंक प्रदान करना आदि। यह आईएमओ का बेहतर तरीका है (अगर मैं लिखता हूं आपको केवल 3 चीजें पूछने के लिए आपको पत्र 1 और केवल बाकी को अनदेखा करते हैं, यह सबसे विनम्र नहीं है, है ना?), तो कोई फोरम नहीं है (फिर, मैं आपको अपमानित करने का मतलब नहीं हूं, लेकिन मुझे लगता है कि यह धारण करता है सच) –

0

यह मेरा विजेट में मेरी ListView पर मेरे लिए काम नहीं करता है, तो मैं एक समाधान के साथ आया था:

सबसे पहले मैं अपने लेआउट परिभाषित FrameLayout जा करने के लिए जहां एक imageView है मेरी पृष्ठभूमि आकर्षित करने के लिए:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/iv_widget_background" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:orientation="vertical" 
     android:padding="@dimen/widget_listview_padding"> 

     <ListView 
      android:id="@+id/lv_widget" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="@null" 
      android:dividerHeight="0dp" /> 
    </LinearLayout> 

</FrameLayout> 

तो मैं इस तरह R.drawable में ड्रॉएबल परिभाषित:

+०१२३५१६४१०६१

red.xml:

 Intent intent = new Intent(context, WidgetProvider.class); 
     intent.setAction("android.appwidget.action.APPWIDGET_UPDATE"); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 
     context.sendBroadcast(intent); 
फिर AppWidgetProvider में मेरी onUpdate() फ़ंक्शन में

मैं निम्नलिखित है:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#F44336" /> 
</shape> 

उपयोगकर्ता SharedPrefs के माध्यम से पृष्ठभूमि में परिवर्तन जब मैं निम्नलिखित क्या करना है :

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWdgetIds) { 
    try { 
     RemoteViews remoteViews; 
     for (int appWdgetId : appWdgetIds) { 
      remoteViews = updateWidgetListView(context, appWdgetId); 
      appWidgetManager.updateAppWidget(appWdgetId, remoteViews); 
     } 
     super.onUpdate(context, appWidgetManager, appWdgetIds); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private RemoteViews updateWidgetListView(Context context, int appWidgetId) { 
    //which layout to show on widget 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 
    Intent svcIntent = new Intent(context, WidgetService.class); 
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 
    remoteViews.setRemoteAdapter(appWidgetId, R.id.lv_widget, svcIntent); 

    setBackground(context, remoteViews); 

    return remoteViews; 
} 

private void setBackground(Context context, RemoteViews remoteViews) { 
    String background = Utils.getStringValue(context, R.string.pref_widget_backgroundcolor_key, 
      R.string.pref_widget_backgroundcolor_default); 

    if (!TextUtils.isEmpty(background)) { 
     int identifier = context.getResources().getIdentifier(
       background, "drawable", context.getPackageName()); 
     remoteViews.setImageViewResource(R.id.iv_widget_background, identifier); 
    } 
} 

परिणाम:

enter image description here

संबंधित मुद्दे