2011-08-11 6 views
20

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

मैं टोस्ट और अन्य संवादों को थीम के गुणों को अवशोषित करने से कैसे रोकूं?

उत्तर

51

आप आसानी से निम्नलिखित कोड द्वारा कस्टम टोस्ट बना सकते हैं:

Toast toast = Toast.makeText(context, resTxtId, Toast.LENGTH_LONG); 
View view = toast.getView(); 
view.setBackgroundResource(R.drawable.custom_bkg); 
TextView text = (TextView) view.findViewById(android.R.id.message); 
/*here you can do anything with text*/ 
toast.show(); 

या आप एक पूरी तरह से कस्टम टोस्ट का दृष्टांत कर सकते हैं:

Toast toast = new Toast(context); 
toast.setDuration(Toast.LENGTH_LONG); 

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.custom_layout, null); 
toast.setView(view); 
toast.show(); 

संवाद अनुरूपण एक अधिक जटिल दिनचर्या है। लेकिन समान कामकाज है।

+3

Javacadabra जवाब मेरी राय में बेहतर है – rubdottocom

+1

मैं सवाल गलत पढ़ रहा हूँ? लेकिन सवाल यह पूछता है कि इसे अनुकूलित करने से कैसे रोकें, और आप इसे अनुकूलित करने के लिए कह रहे हैं? – WORMSS

+0

@WORRMS, आप सही हैं, लेकिन ... जहां तक ​​थीम बदल दी गई है, कोई भी टोस्ट जो इस विषय को लागू नहीं करता है वह एक कस्टम टोस्ट है (क्योंकि हमें इसे फिर से "फिर से स्टाइल" करने की आवश्यकता है) – Dmitry

4

यहां पूर्ण उदाहरण आता है, जिसका उपयोग गतिविधियों में अनुकूलित टोस्ट के लिए किया जा सकता है।

displayToast

// display customized Toast message 
    public static int SHORT_TOAST = 0; 
    public static int LONG_TOAST = 1; 
    public static void displayToast(Context caller, String toastMsg, int toastType){ 

     try {// try-catch to avoid stupid app crashes 
      LayoutInflater inflater = LayoutInflater.from(caller); 

      View mainLayout = inflater.inflate(R.layout.toast_layout, null); 
      View rootLayout = mainLayout.findViewById(R.id.toast_layout_root); 

      ImageView image = (ImageView) mainLayout.findViewById(R.id.image); 
      image.setImageResource(R.drawable.img_icon_notification); 
      TextView text = (TextView) mainLayout.findViewById(R.id.text); 
      text.setText(toastMsg); 

      Toast toast = new Toast(caller); 
      //toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
      toast.setGravity(Gravity.BOTTOM, 0, 0); 
      if (toastType==SHORT_TOAST)//(isShort) 
       toast.setDuration(Toast.LENGTH_SHORT); 
      else 
       toast.setDuration(Toast.LENGTH_LONG); 
      toast.setView(rootLayout); 
      toast.show(); 
     } 
     catch(Exception ex) {// to avoid stupid app crashes 
      Log.w(TAG, ex.toString()); 
     } 
    } 

और toast_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toast_layout_root" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="10dp" 
       android:background="#DAAA" 
       > 
    <ImageView android:id="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_marginRight="10dp" 
       /> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 
25

मुझे पता है सवाल का जवाब दे दिया गया है और बाद इस स्तर पर काफी पुराना है। हालांकि मैंने सोचा कि मैं उन लोगों के लिए उत्तर छोड़ दूंगा जो इस प्रश्न में आते हैं।

Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show(); 

के रूप में इस का विरोध किया (संदेश मानते हुए एक दृश्य के भीतर से बुलाया जा रहा है):

मैं इस मुद्दे को आज और जिस तरह से मैं का समाधान इसे इस तरह मेरी टोस्ट संदेशों प्रदर्शित करके था के साथ मुसीबत में भाग गया :

Toast.makeText(v.getContext(), "Checking login details...", Toast.LENGTH_SHORT).show(); 

यह मेरे द्वारा किए गए मुद्दों को साफ़ करता है। वैसे भी उम्मीद है कि यह मदद करता है। यहां इसी विषय पर मेरे प्रश्न का लिंक दिया गया है।

Toast background color being changed

+1

सुपर। धन्यवाद!! – OWADVL

+1

महान टिप्पणी, धन्यवाद! बीटीडब्ल्यू, पहला और स्वीकृत उत्तर मेरे लिए काम नहीं करता था, लेकिन आपका समाधान करता है। – middlehut

+1

धन्यवाद, मेरे पास ओपी के समान प्रश्न था और यह मेरे लिए बहुत अच्छा काम करता था! – deanresin

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

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