在Android Spinner中设置文本颜色

12

我想要在我的Android Spinner中更改显示的选定项的颜色(目标API级别为16及以上)。我已经尝试了在SO上发布的几种解决方案,包括为我的Spinner项创建自定义布局并将ColorStateList用作自定义布局的文本颜色属性,但没有成功。 Spinner显示在半透明背景上 - 因此项目的自定义布局不起作用,因为它向Spinner添加了颜色。 目前我的hack解决方案是:

if (_colorCodeSpinner.getSelectedView() != null) {
    ((TextView) _colorCodeSpinner.getSelectedView()).setTextColor(0xFFFFFFFF);
}

但仅当所选视图不为空时才有效(在屏幕方向更改时为空)。

我无法相信没有一个简单的解决方案来设置文本颜色。这似乎是您经常需要做的事情。同样,改变箭头的颜色也是如此,目前我通过

_colorCodeSpinner.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

我有什么遗漏吗?怎么更改Spinner上的颜色是推荐的方法?

Android spinner

如图所示,Spinner中显示所选项目的文本颜色为黑色,但我想要将其改成白色。

编辑

澄清一下:我不是在寻找一些在运行时覆盖值的小代码片段(如我在此问题中发布的两个片段)。我正在寻找一种真正正确的方法来完成这个操作(例如在XML布局或通过主题设置)。只需设置一次文本颜色属性,以便我无需每次选择项目时都进行更新。

7个回答

12

这样做:

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
          ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE); /* if you want your item to be white */
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      }
  });

这太棒了,它对我来说比其他的都好用。非常感谢@HugoHouyez。 - Ali

3
你可以通过编辑styles.xml布局文件来实现这个目标。在这个答案中,我使用了一个在Android Studio中创建的新项目,其中minSdkVersion为16,AppCompatSpinner。

styles.xml布局:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:spinnerItemStyle">@style/mySpinnerItemSelectedStyle</item>
</style>

<style name="mySpinnerItemSelectedStyle" parent="@android:style/Widget.Holo.TextView.SpinnerItem">
    <item name="android:textColor">@color/spinnerTextColor</item>
</style>

并在colors.xml文件中添加以下内容:

<color name="spinnerTextColor">#ffffff</color>

该解决方案取自下面的链接。虽然它是用于颜色旋转器下拉项,但大多数情况下采用相同的方法。

https://dev59.com/LWox5IYBdhLWcg3wCQAq#22207394


2
这个对你有用。
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    item = (String) parent.getItemAtPosition(arg2);
   ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);



    }

或者
您可以使用选择器来更改颜色

创建一个名为my_selctor.xml的xml文件。

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="black" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="black" /> <!-- focused -->
     <item android:color="white" /> <!-- default -->
 </selector>

并且在你的文本视图中设置它,就像这样

<TextView ...........
   android:textColor=""@drawable/my_selctor"/>

2
请尝试以下代码:
XML:
   <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_margin="20dp"
        android:popupBackground="#ffffff"
        android:layout_height="match_parent">

    </Spinner>

创建另一个用于 TextView 的 XML。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="dshsgv"
android:padding="5dp"
android:textColor="#000000">

</TextView>

然后在您的活动中:

public class MainActivity extends AppCompatActivity {
Spinner spinner;
String[] cat = {"Automobile", "Automobile"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adpter = new ArrayAdapter<String>     (MainActivity.this, R.layout.text, cat);
    spinner.setAdapter(adpter);
 }
 }

2

声明ArrayAdapter并将其设置为您的Spinner,代码如下:

ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
                            R.layout.simple_spinner_dropdown_item, your_strings);
adapter_state.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
_colorCodeSpinner.setAdapter(adapter_state);

并且布局xml文件simple_spinner_dropdown_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"
    android:textColor="#AAA"
    android:padding="5dp"
    />

这对我有用。

0

请跟随此链接

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

       ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
       ((TextView) parent.getChildAt(0)).setTextSize(12);

    }

    public void onNothingSelected(AdapterView<?> parent) {

    }
};

0
你可以像这样使用。这将更改您的DropDown菜单图标。
spinner.getBackground().setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);

然后创建一个名为 spinner_text.xmlTextView 布局,像这样

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerText"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#fff" />

并将此代码编写到您的MainActivity.java类中,如下所示:

 List<String> categories = new ArrayList<String>();
    categories.add("Automobile");
    categories.add("Business Services");
    categories.add("Computers");
    categories.add("Education");
    categories.add("Personal");
    categories.add("Travel");

    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, categories);
    spinner.setAdapter(adapter);
    spinner.getBackground().setColorFilter(ContextCompat.getColor(this,R.color.white), PorterDuff.Mode.SRC_ATOP);
    // attaching data adapter to spinner
    spinner.setAdapter(adapter); 

它改变了下拉框的背景颜色而不是文本颜色。 - Adnan haider

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