2012-09-21 33 views
5


में आइटम का टेक्स्ट रंग कैसे सेट करें मुझे स्पिनर के लिए टेक्स्ट रंग सेट करने में समस्याएं आ रही हैं। मैंने कुछ उदाहरण देखे हैं लेकिन में strings.xml से अधिकांश में ऐरेएडाप्टर और स्ट्रिंग सरणी है क्योंकि मेरे स्पिनर के आइटम SQLite डेटाबेस से पुनर्प्राप्त किए गए हैं, इसलिए मुझे लगता है कि इससे मदद नहीं मिल सकती है।
स्पिनर एंड्रॉइड

यहाँ मेरी स्पिनर के कोड हैं PersonalInformation.java

public class PersonalInformation extends Activity 
{ 
    EditText txtLikes, txtDislikes, txtType, txtDate; 
    Button btnView, btnBack; 
    Spinner nameSpinner;  

    final Context context = this; 

    private int namesSpinnerId;   

    LikesDBAdapter likeDB = new LikesDBAdapter(this); 
    DislikesDBAdapter dlikeDB = new DislikesDBAdapter(this); 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.info); 

     BuddyDBAdapter buddyDB = new BuddyDBAdapter(this); 
     buddyDB.open(); 

     Cursor friendsCursor = buddyDB.getAllNames(); 
     startManagingCursor(friendsCursor); 

     String[] from = new String[]{BuddyDBAdapter.KEY_NAME}; 
     int[] to = new int[]{R.id.name}; 

     SimpleCursorAdapter friendsCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, friendsCursor, from, to); 
     friendsCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     nameSpinner = (Spinner)findViewById(R.id.nameSpinner); 
     nameSpinner.setAdapter(friendsCursorAdapter); 
     //buddyDB.close(); 


     nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
      { 
       @Override 
       public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
       { 
        Cursor c = (Cursor)parent.getItemAtPosition(pos); 
        namesSpinnerId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID)); 
       } 

       @Override 
       public void onNothingSelected(AdapterView<?> parent) 
       { 
        // TODO Auto-generated method stub 

       } 
      }); 
     buddyDB.close(); 

और, इन info.xml में स्पिनर के लेआउट कोड हैं

<Spinner 
     style="@style/SpinnerStyle" 
     android:id="@+id/nameSpinner"   
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:prompt="@string/friends_prompt" 
     android:textColor="@color/green" /> 

कृपया मुझे कैसे में आइटम पाठ रंग सेट करने के साथ मदद स्पिनर।
मैं प्रदान की गई किसी भी मदद की सराहना करता हूं। धन्यवाद।! =)

+1

कृपया इस लिंक को जांचें। http://stackoverflow.com/a/6661762/614807 –

उत्तर

9

अपने स्पिनर आइटम के लिए xml फ़ाइल बनाएं। और लेआउट फ़ोल्डर

spinner_view.xml: 


<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="left" 
    android:textColor="@color/green"   
/> 

और अंततः आपके कोड में डालें।

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_view,yourList); 
+1

आपके उत्तर ने भी मेरी मदद की। धन्यवादएसएस =) – Preeyah

+0

अभी भी कोई रंग परिवर्तन नहीं .. :( –

0

संशोधित/लेआउट फ़ोल्डर

<TextView android:id="@+id/spinnerText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:textColor="#2f4f4f" 
    android:textSize="20dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"/> 

पाठ रंग आपके रेस में एक नया xml फ़ाइल बना सकते हैं और इस तरह के समान एडाप्टर फोन:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinner_array, 
       R.layout.spinner_text); 
0

यह simple_spinner_drowdown_item.xml के स्रोत कोड है

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml 
** 
** Copyright 2008, The Android Open Source Project 
** 
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
** 
**  http://www.apache.org/licenses/LICENSE-2.0 
** 
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License. 
*/ 
--> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    style="?android:attr/spinnerDropDownItemStyle" 
    android:singleLine="true" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/dropdownListPreferredItemHeight" 
    android:ellipsize="marquee" 
    android:textAlignment="inherit"/> 

समस्या यहाँ है एंड्रॉयड: attr/dropdownListPreferredItemHeight सार्वजनिक नहीं है, लेकिन यह यह 48 डुबकी या 64dip है लगता है: https://android.googlesource.com/platform/frameworks/base/+/414c4984fdbb03b688bb5c3c76d20100fce3d067%5E1..414c4984fdbb03b688bb5c3c76d20100fce3d067/

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