2012-09-21 29 views
5


Tôi gặp sự cố khi đặt màu văn bản cho Spinner. Tôi đã nhìn thấy vài ví dụ nhưng hầu hết có mảng ArrayAdapter và String từ strings.xml trong res folder vì các mục của Spinner của tôi được lấy từ Cơ sở dữ liệu SQLite vì vậy tôi nghĩ rằng nó có thể không hữu ích.
Cách đặt màu văn bản của các mục trong Spinner Android

Dưới đây là mã Spinner tôi 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(); 

Và, đây là những mã bố trí Spinner trong 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" /> 

Xin hãy giúp tôi với làm thế nào để thiết lập màu sắc mục văn bản trong Spinner.
Tôi sẽ đánh giá cao bất kỳ trợ giúp nào được cung cấp. Cảm ơn.! =)

+1

Hãy kiểm tra liên kết này . http://stackoverflow.com/a/6661762/614807 –

Trả lời

9

Tạo tệp xml cho mục xoay của bạn. và đặt nó vào thư mục bố cục

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"   
/> 

và cuối cùng là mã của bạn.

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

Câu trả lời của bạn cũng giúp tôi. Thanksss =) – Preeyah

+0

Vẫn không thay đổi màu sắc .. :( –

0

thay đổi màu sắc văn bản tạo ra một file xml mới trong res của bạn/bố trí thư mục

<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"/> 

và gọi bộ chuyển đổi tương tự như thế này:

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

Đây là mã nguồn của 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"/> 

Vấn đề ở đây là android:? Attr/dropdownListPreferredItemHeight không được công khai, nhưng có vẻ như nó là 48 nhúng hoặc 64dip: https://android.googlesource.com/platform/frameworks/base/+/414c4984fdbb03b688bb5c3c76d20100fce3d067%5E1..414c4984fdbb03b688bb5c3c76d20100fce3d067/

Các vấn đề liên quan