如何在微调器中设置所选的项目颜色?

问题描述 投票:4回答:3
<Spinner
     android:id="@+id/spinner1"
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"   
     android:layout_alignLeft="@+id/editText1F"
     android:layout_alignTop="@+id/txtLabel1F" 
     android:entries="@array/cat_array"  
     android:prompt="@string/cat_promt" 
     android:textColor="#ffffff"                            
   />

我的背景为黑色,显示的项目为灰色bg和黑色字体颜色。但是当它出现在选定位置时,它会将字体显示为黑色,因此无法看到。如何更改其中的颜色?

android xml spinner
3个回答
4
投票

您需要创建自定义微调器布局以实现所需。

看看这些问题,他们有你想要的答案:

How to customize a Spinner in Android

Android: Custom Spinner Layout

我们的想法是为您的行创建一个布局,并在代码中创建一个带有适配器的微调器时设置它。


1
投票

最简单的用法

使用它来更改所选文本的文本

YOUR_SPINNER.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
     TextView selectedText=  view.findViewById(R.id.text_view_name_in_Adapter);
     selectedText.setTextColor(getResources().getColor(R.color.YOUR_COLOR));
    }
}

0
投票

如果你正在使用MaterialBetterSpinner并绑定你的布局, 试试这个,希望它可以帮助你:

public class MyAdapter extends ArrayAdapter<String> {      

    public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);           

    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
        rowBinding.tv1.setText(mMy.getValues().get(position));
        if(position == mMy.getCurrentIndex()) {
            rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
            rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
        }
        return rowBinding.getRoot();
    }
}

您的XML应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/colorBackgroundStart">
        <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_weight="0.7"
            android:layout_height="30dp"
            android:textColor="#fff"
            android:textSize="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="8dp"/>
    </LinearLayout>
</layout>

使用此适配器和yourXML创建一个微调器:

final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());

final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
© www.soinside.com 2019 - 2024. All rights reserved.