闭合旋转器的文本颜色

23
我知道关闭的旋转器实际上是一个 View。但我猜测它有一个 TextView 来显示文本。我如何访问那个 TextView 以便我可以改变文本颜色?
编辑:我需要在程序中动态地改变它,而不是在 XML 中。
TextView v = (TextView) getView(mySpinner);

v.setTextColor(.....

这样不起作用...

谢谢!

    array_typ=new String[5];
    array_typ[0]="Pressure";
    array_typ[1]="Level";

    array_typ[2]="Overage";
    array_typ[3]="Under";
    array_typ[4]="Taken";


    adaptertyp = new ArrayAdapter<Object>(this,R.layout.simple_spinner_item, array_typ);
    typ.setAdapter(adaptertyp);

你不能在XML文件中定义颜色吗? - gnclmorais
6个回答

34

要修改文本颜色,请在您的res/layout文件夹中创建一个新的xml文件(例如my_spinner_text.xml)。将一个TextView添加到新的xml文件中,并对其进行您想要的修改:

要修改文本颜色,需在你的 res/layout 文件夹下创建一个新的 xml 文件(例如 my_spinner_text.xml)。在新的 xml 文件中添加一个 TextView 并修改您所需的样式:

<TextView android:id="@+id/spinnerText" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true"
    android:textColor="#CCCCCC" 
    android:textSize="20dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"/>
创建一个ArrayAdapter,使用新的TextView并将其设置为你的spinner:
    Spinner localSpinner = (Spinner)findViewById(R.id.mySpinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner_array,
                R.layout.my_spinner_text);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    localSpinner.setAdapter(adapter);

1
对我来说,我必须在xml文件中添加一个命名空间 <?xml version="1.0" encoding="utf-8"?><TextView android:id="@+id/spinnerText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="#CCCCCC" android:textSize="20dp" xmlns:android="http://schemas.android.com/apk/res/android"/> - ala
4
您提供的网站似乎已经下线。 - rekire
@ala请修复您评论中的链接。 - Diederik
@Diederik 如果你指的是 http://schemas.android.com/apk/res/android。那不是一个URI链接,而只是XML元素的模式命名空间,它并不意味着要指向任何网站。添加命名空间是Eclipse建议解决我遇到的问题。我更新了Vito的答案以包括该命名空间。然而,如果你指的是 http://www.designerandroid.com/?p=28,我恐怕我没有建议这个链接。也许Vito可以帮忙。 - ala

23

您可以在setOnItemSelectedListener事件中更改文本颜色或访问textview。

            spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                 ((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249));   

            }

这个有效!Budius建议的不行,因为我无法设置文本颜色。 - Someone Somewhere
这看起来很低效 - 每次都设置颜色。你可以通过扩展ArrayAdapter并重写getDropDownView()一次性在Spinner中的所有项目上设置颜色。 - IgorGanapolsky

9
我知道关闭的Spinner实际上是一个View。确切地说,它是您告诉SpinnerAdapter创建的任何内容。
但我猜测它有一个TextView用于显示文本。这取决于您告诉SpinnerAdapter创建什么。
如何访问该TextView以更改文本颜色?理想情况下,您不需要这样做 - 通过告诉SpinnerAdapter创建正确的颜色。如果颜色有所不同,请在SpinnerAdapter中覆盖getView()并在那时更改颜色。
如果必要,可以尝试调用getSelectedView()以获取由关闭的Spinner显示的当前View,但是您在此处进行的任何更改可能会在用户的下一次选择时被消除,如果较早的View被回收,则备用颜色可能会稍后返回。

我已经添加了我正在创建的旋转器。我对Android和Java非常陌生,所以即使您的答案听起来像有一个答案,我也不知道该输入什么。你能给我展示几行代码让我改变文本颜色吗?谢谢! - Mark Worsnop
@Mark Worsnop:“我已经添加了我如何创建上面的旋转器。”--查看您的R.layout.simple_spinner_item资源并更改其中的颜色。 - CommonsWare
@Mark Worsnop:那么您需要重写getView()并在该过程中自定义TextView的颜色。在此评论的末尾,附有我一本书的免费摘录,其中讨论了这个过程。它是在ListView的上下文中进行的,但是相同的概念也适用于这里。http://commonsware.com/Android/excerpt.pdf - CommonsWare
谢谢,我非常感激你的帮助!感谢你抽出时间! - Mark Worsnop

1

如果要以编程方式完成,您需要扩展适配器类,类似于以下内容:

    ArrayAdapter<CharSequence> adapter = new ArrayAdater(this){
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
           View v = super.getView(position, convertView, parent);           
           // change the color here of your v
           v.  ... etc. etc        
        }
    }

我看到了 setBackgroundColor(),但没有 setTextColor() - Someone Somewhere

0

要更改关闭的Spinner的文本颜色,我是这样做的,它可以正常工作

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
  View view = convertView;
  if (view == null) {
    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.context_row_icon, null);
  }
  TextView mTitle = (TextView) view.findViewById(R.id.context_label);
  ImageView flag = (ImageView) view.findViewById(R.id.context_icon);                

  mTitle.setText(values[position].getLabel(activity));

  if (!((LabelItem) getItem(position)).isEnabled()) {
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
  } else {
    mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
  }
  return view;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = convertView;
  if (view == null) {
    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.context_row_icon, null);
  }
  TextView mTitle = (TextView) view.findViewById(R.id.context_label);
  ImageView flag = (ImageView) view.findViewById(R.id.context_icon);                

  mTitle.setText(values[position].getLabel(activity));
  mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
  return view;
}

-1

尝试一下这个:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ((TextView)parent.getChildAt(0)).setTextColor(getResources().getColor(R.color.colorPrimary));


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