Android更改Spinner

时间:2017-04-05 13:57:59

标签: android spinner

我一直在尝试更改Spinner TextView颜色。我需要以编程方式进行。我有我的个人适配器和资源视图。 (我不想更改弹出的sipnner文本颜色,只选择在选择somenthig时在用户布局上显示的文本)

这是我的代码:

PersonalArrayAdapter spAdapterInfraccion = new PersonalArrayAdapter(getContext(),
            R.layout.item_spinner,
            infraccionList);

spinner.setAdapter(spAdapter);

item_spinner.xml:

<?xml version="1.0" encoding="utf-8"?>
<co.my.app.utils.CustomTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

这是我尝试的方式,但返回null。

CustomTextView text = (CustomTextView)view.findViewById(R.id.text1);
text.setTextColor(getContext().getColor( R.color.black ));

1 个答案:

答案 0 :(得分:1)

你可以试试这个: -

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
int index = adapterView.getSelectedItemPosition();
((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue));

希望它可以帮到你。 :)