在Android Spinner中如何设置选项的文本颜色

5


我遇到了设置Spinner文本颜色的问题。我看过一些例子,但是大多数都使用ArrayAdapter和res文件夹中的String数组从strings.xml获取Spinner的项,因为我的Spinner项是从SQLite数据库中检索的,所以我认为这可能没有帮助。

下面是PersonalInformation.java中的Spinner代码:

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的布局代码:

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

请帮我设置Spinner中的项目文本颜色。非常感谢您提供的任何帮助。谢谢!=)

1
请查看此链接:https://dev59.com/TG855IYBdhLWcg3waDeR#6661762。 - Chirag
4个回答

9
创建一个 XML 文件用于您的 Spinner 项目,并将其放置在 layout 文件夹中。
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);

仍然没有颜色变化.. :( - Vivek Av

2

0

修改文本颜色,在您的res/layout文件夹中创建一个新的xml文件

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

并调用类似于这样的适配器:

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

这里的问题是?android:attr/dropdownListPreferredItemHeight不是公共的,但它似乎是48dip或64dip: https://android.googlesource.com/platform/frameworks/base/+/414c4984fdbb03b688bb5c3c76d20100fce3d067%5E1..414c4984fdbb03b688bb5c3c76d20100fce3d067/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接