2017-04-05 171 views
1

我一直在尝试更改Spinner TextView颜色。我需要通过编程来完成。我有我的个人适配器和资源视图。 (我并不想改变弹出sipnner文本颜色,只有被选中somenthig当用户布局上显示的文本)Android更改Spinner的TextView颜色

这是我的代码:

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)); 
+0

没有看到'view'是什么 – Flikk

回答

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)); 

希望它可以帮助你。 :)