在将状态设置为禁用时,如何在不使用适配器的情况下以编程方式更改微调项目文本颜色

时间:2016-02-11 08:23:01

标签: android spinner

我在我的应用程序中使用微调器并通过xml设置其项值。我已经通过样式设置了微调项目的默认颜色,我必须根据我的应用程序的需要启用和禁用我的微调器。 我想在设置其状态禁用时以编程方式更改其项目颜色。我通过谷歌搜索但找不到任何解决方案。是否有任何人对此有任何想法如何设置它。我使用了这个链接how do you set the spinner text color?,但它给出了空指针异常。

    m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);

1 个答案:

答案 0 :(得分:0)

您可以为您的微调器项目创建自定义布局,如下所示:

How to change spinner text size and text color?

并在ArrayAdapter中使用它。

现在,根据您的要求,您必须创建两个xml文件,为微调器项提供两种不同的颜色:

1)当你的微调器启用时。

2)当你的微调器被禁用时。

只需将以下行更改为:

if(spinner1.isEnabled){
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item1,list);
}else{
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item2,list);
}
相关问题