2011-07-14 14 views
7

मेरे पास मेरे आवेदन में AlertDialog है। इसमें TextView विजेट के साथ कस्टम दृश्यों की एक सूची शामिल है। सबकुछ एंड्रॉइड 2.x पर ठीक काम करता है। AlertDialog सफेद सूची और काले रंग के पाठ के साथ बनाया गया है। लेकिन जब मैं एंड्रॉइड 3.x उपकरणों पर अपना ऐप चलाता हूं तो सभी TextView एस काले होते हैं और सूची की पृष्ठभूमि भी काला होती है। इसलिए जब तक मैं आइटमों में से किसी एक को टैप और पकड़ नहीं लेता तब तक मैं टेक्स्ट नहीं देख सकता।एंड्रॉइड: अलर्टडिअलॉग में सूची आइटमों के लिए टेक्स्ट रंग कैसे सेट करें

<TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:textAppearance="?android:attr/textAppearanceSmallInverse" /> 

मैंने सोचा था कि textAppearance विशेषता के लिए textAppearanceSmallInverse का उपयोग करके पाठ पैरामीटर सेट करने के लिए एक उचित तरीका है और यह सभी उपकरणों पर काम करना चाहिए लेकिन लगता है मैं था:

यहाँ लेआउट फ़ाइल से एक 'TextView परिभाषा है गलत। तो मुझे AlertDialog डिस्प्ले सूची आइटम को सभी प्लेटफ़ॉर्म पर ठीक से करने के लिए क्या करना चाहिए? अग्रिम में धन्यवाद।

+1

यह प्रश्न काफी पुराना है, लेकिन हो सकता है कि किसी और से इसका फायदा होगा। समाधान जो आप ढूंढ रहे हैं उसे प्राप्त करने के लिए एंड्रॉइड के अंतर्निहित संसाधन चयन का लाभ उठाना है। [यह] देखें (http://kmansoft.com/2011/10/15/alertdialog-custom-listview-items-and-honeycomb/) निर्देशों के लिए ट्यूटोरियल। – howettl

+0

मैंने अपने प्रोजेक्ट में बिल्कुल वही दृष्टिकोण इस्तेमाल किया, लेकिन उम्मीद थी कि एक बेहतर समाधान मौजूद होगा। – Michael

+0

मेरा जवाब क्यों हटाया गया था? – howettl

उत्तर

2

समाधान लाभ उठाने के लिए है एंड्रॉयड के अंतर्निहित संसाधन चयन प्रणाली। आपको दो अलग-अलग शैलियों को निर्दिष्ट करना चाहिए और उन्हें एपीआई संस्करण के आधार पर उचित फ़ोल्डर में रखना चाहिए। ध्यान दें कि निम्नलिखित उदाहरण मेरा नहीं हैं, मैंने उन्हें this ट्यूटोरियल से लिया।

res/values-v4/styles.xml:

<resources> 

<!-- Text for listboxes, inverted for Andorid prior to 3.0 --> 

<style name="MyListTextAppearanceSmall"> 
    <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item> 
</style> 

<style name="MyListTextAppearanceDefault"> 
    <item name="android:textAppearance">?android:attr/textAppearanceInverse</item> 
</style> 

<style name="MyListTextAppearanceMedium"> 
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item> 
</style> 
</resources> 

res/values-v11/styles.xml:

<resources> 
    <!-- Text for listboxes, non-inverted starting with Android 3.0 --> 

    <style name="MyListTextAppearanceSmall"> 
     <item name="android:textAppearance">?android:attr/textAppearanceSmall</item> 
    </style> 

    <style name="MyListTextAppearanceDefault"> 
     <item name="android:textAppearance">?android:attr/textAppearance</item> 
    </style> 

    <style name="MyListTextAppearanceMedium"> 
     <item name="android:textAppearance">?android:attr/textAppearanceMedium</item> 
    </style> 
</resources> 

फिर, अपने TextView में है, इसलिए जैसे शैली निर्दिष्ट करें:

<TextView 
    android:style="@style/MyListTextAppearanceSmall" 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true" 
    android:ellipsize="marquee" /> 

ट्यूटोरियल एक lengthier विवरण के लिए ऊपर लिंक देखें ।

0

ऐसा शायद होता है क्योंकि आप थीम निर्दिष्ट नहीं कर रहे हैं, तो यह डिफ़ॉल्ट थीम पर वापस आ जाता है। 2.x में यह थीम होना चाहिए। ब्लैक और 3.x थीम में। होलो (या थीम। लाइट, इस पर यकीन नहीं है)। फिर textAppearanceSmallInverse प्रत्येक विषय में अलग शैली का समाधान करता है। यहाँ

// Sets dialog for popup dialog list 
AlertDialog dialog; 
String[] items = {"exampleItem"}; 
ListAdapter itemlist = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Title"); 
builder.setAdapter(itemlist, new DialogInterface.OnClickListener() 
{ 
    public void onClick(DialogInterface dialog, int item) 
    { 
    } 
}); 
dialog = builder.create(); 
dialog.getListView().setBackgroundColor(Color.WHITE); 

, आप सूचीदृश्य हो रही है और सफेद करने के लिए पृष्ठभूमि रंग सेट कर रहे हैं:

+0

मैं 2.x में 'थीम' का उपयोग कर रहा हूं और 3.x में 'थीम.होलो' का उपयोग कर रहा हूं। लेकिन समस्या यह है कि 'अलर्टडिअलॉग' विभिन्न प्लेटफार्मों पर विपरीत शैलियों का उपयोग करता है। लेकिन दोनों प्लेटफार्म शीर्षक टेक्स्ट के लिए एक ही शैली का उपयोग करते हैं। यह अजीब व्यवहार है। – Michael

1

पॉपअप संवाद के लिए आपका कोड इस के समान दिखना चाहिए। आप इस मामले काले रंग में, textviews तो आप TextView के लेआउट में उनके रंग परिभाषित करने की जरूरत से प्रत्येक के लिए पाठ का रंग बदलना चाहते हैं:

android:textColor="#000000" 
+0

आपकी प्रतिक्रिया के लिए धन्यवाद, लेकिन मैं सूची की पृष्ठभूमि को बदलना नहीं चाहता हूं। मैं चाहता हूं कि ऐसा लगता है कि इसे किसी विशेष मंच पर देखना चाहिए। मैं चाहता हूं कि प्रत्येक मंच पर इसे पठनीय बनाने के लिए उचित टेक्स्ट शैली निर्धारित करें। – Michael

1

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

dialogBuilder.setInverseBackgroundForced(true); 

समस्या ठीक हल करती है।

+0

जहां तक ​​मुझे यह ध्वज याद है, एंड्रॉइड 2.x और एंड्रॉइड 3.x दोनों पर समस्या का समाधान नहीं करता है। – Michael

+1

setInverseBackgroundForced अब बहिष्कृत है –

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