简体   繁体   English

在 Android Spinner 上将文本颜色更改为所选项目

[英]Change text color to selected item on Android Spinner

I have an spinner which has all it's options in black color.我有一个微调器,它的所有选项都是黑色的。 What I want is to change to white the selected item that's showing in the activity, but NOT in the dropDownView, there it has to keep beeing everything black, just when it is beeing displayed as the selected item, I want it white.我想要的是将活动中显示的选定项目更改为白色,但不是在 dropDownView 中,它必须保持所有黑色,就在它显示为选定项目时,我希望它是白色的。

My spinner:我的微调器:

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

My xml:我的xml:

<Spinner
  android:id="@+id/spinner"
  android:layout_width="match_parent"
  android:layout_height="34dp"
  android:layout_marginTop="30dp"
  android:background="@drawable/border_thicker" />

I know that the spinner has this method我知道微调器有这个方法

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
         // Your code here
     } 

     public void onNothingSelected(AdapterView<?> adapterView) {
         return;
     } 
}); 

But I don't really know how to apply it to what I want.但我真的不知道如何将它应用到我想要的东西上。 Please help!!请帮忙!!

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
       public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
     ((TextView)parent.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF")); 
        }   

      public void onNothingSelected(AdapterView<?> adapterView) {
       return;
       } 
 }); 

Create a custom layout say spinner_item.xml with a simple TextView with white Text color使用带有白色文本颜色的简单TextView创建自定义布局,例如spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white">
</TextView>

Then set the spinner layout to the new customized item:然后将微调器布局设置为新的自定义项:

ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, R.layout.spinner_item, myList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

No need to change anything in setOnItemSelectedListener无需更改setOnItemSelectedListener中的任何内容

If you want to change the text color of the selected option in a Spinner you have to add this:如果要更改 Spinner 中所选选项的文本颜色,则必须添加以下内容:

yourSpinner.setSelection(0, true);
View view = yourSpinner.getSelectedView();
((TextView) view).setTextColor(yourColor);

It's important to add the true parameter at the setSelection function, otherwise view will be null .setSelection函数中添加true参数很重要,否则view将为null

Don't forget to change the color of the text at onItemSelected too:不要忘记在onItemSelected上更改文本的颜色:

yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       // Change the selected item's text color 
       ((TextView) view).setTextColor(yourColor);
    } 

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

    } 

});

Explained here: https://stackoverflow.com/a/33910094/19401165在这里解释: https ://stackoverflow.com/a/33910094/19401165

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM