2013-07-23 24 views
6

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

if (seriesSelection == null) { 
    Toast toast = Toast.makeText(getApplicationContext(), "tidak ada chart yang dipilih", Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.CENTER, 50, 50); 
    toast.getView().setPadding(10, 10, 10, 10); 
    toast.getView().setBackgroundColor(Color.RED); 
    TextView text = (TextView) toast.getView().findViewById(android.R.id.message); 
    text.setTextColor(Color.WHITE); 
    text.setTextSize(14); 
} else { 
    Toast toast= Toast.makeText(
      getApplicationContext(), 
      "Nilai " + listData.get(seriesSelection.getPointIndex()).getInuNilai()+ 
      " tanggal " + listData.get(seriesSelection.getPointIndex()).getTanggal(), 
      Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.CENTER, 50, 50); 
    toast.getView().setPadding(10, 10, 10, 10); 
    toast.getView().setBackgroundColor(Color.RED); 
    text.setTextColor(Color.WHITE); 
    text.setTextSize(14); 
    toast.show(); 
} 
+0

मेरे लिए, 'अन्य' केस लागू पैडिंग के साथ सफेद पाठ के साथ लाल टोस्ट दिखाता है। – sandrstar

उत्तर

9

आप कस्टम दृश्य को कस्टम दृश्य बढ़ा सकते हैं और toast.setView(layout) का उपयोग कर सकते हैं।

उदाहरण:

LayoutInflater inflater = getLayoutInflater(); 
View layout = inflater.inflate(R.layout.custom_toast, 
           (ViewGroup) findViewById(R.id.toast_layout_root)); 

TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("This is a custom toast"); 

Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
toast.setDuration(Toast.LENGTH_LONG); 
toast.setView(layout); 
toast.show(); 

और अपने xml

<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="8dp" 
       android:background="#DAAA" 
       > 
    <ImageView android:src="@drawable/droid" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="8dp" 
       /> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       /> 
</LinearLayout> 

अधिक जानकारी @

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Ran अपने अगर और कोड के किसी और हिस्से (अलग से) यह टोस्ट से पता चलता लाल पृष्ठभूमि और सफेद पाठ रंग के साथ। मुझे कोई समस्या नहीं दिख रही है। लेकिन अगर आपको कस्टमाइज़ करने की ज़रूरत है तो आप एक कस्टम लेआउट का उपयोग कर सकते हैं और लेआउट को बढ़ा सकते हैं और दृश्य को टोस्ट पर सेट कर सकते हैं।

संपादित करें:

आपका TextView

TextView text = (TextView) toast.getView().findViewById(android.R.id.message); 

में अगर हिस्सा और बाकी हिस्सा TextView में आरंभ नहीं किया है आरंभ नहीं हो जाता।

अगर और अन्य कोड के बाहर टेक्स्टव्यू प्रारंभ करें।

चेक इस पुस्तकालय बुलाया crouton जो आपको पता चल सकता उपयोगी

https://github.com/keyboardsurfer/Crouton

+0

मैं डिफ़ॉल्ट टोस्ट को संशोधित करके कस्टम लेआउट बनाने के बिना अपने टोस्ट को कस्टमाइज़ करना चाहता हूं। क्या मैं? –

+1

@AoyamaNanami आप उपरोक्त पद की जांच कर सकते हैं। आपका कोड भी ठीक काम करता है मैं लाल पृष्ठभूमि और सफेद पाठ के साथ टोस्ट देख सकता हूं। तो सही समस्या क्या है। मैं आपकी समस्या का पुनरुत्पादन नहीं कर सकता। मैंने चिपकाकर कॉपी करके अपना कोड चलाया। यह बढ़िया काम करता है। कस्टम लेआउट का उपयोग आपको अनुकूलित करने के अधिक विकल्प देगा। – Raghunandan

+0

यह काम करता है .. धन्यवाद :) –

2

टोस्ट एक setView() विधि है:

यह कैसे मैं अपने टोस्ट अनुकूलित है।

आप किसी भी दृश्य को दिखाने के लिए टोस्ट को कस्टमाइज़ कर सकते हैं।

मैं टोस्ट के अंदर दृश्य को संपादित करने की कोशिश करने के बजाय कहूंगा, आप बस एक दृश्य बनाएं और इसे स्वयं में पॉप करें।

+0

क्या आप बता सकते हैं कि इसे कैसे अनुकूलित किया जाए? –

0

मैं बहुत सरल और आसान कोड के हिसाब से एक टोस्ट को अनुकूलित के लिए है, तो आप भी टोस्ट और पाठ रंग की पृष्ठभूमि बदल सकते हैं।

Toast toast = Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG); 
    View view1 = toast.getView(); 
    toast.getView().setPadding(20, 20, 20, 20); 
    view1.setBackgroundResource(R.color.GREEN); 
    view1.setTextColor(Color.RED); 
    toast.show(); 
संबंधित मुद्दे