我在布局中有一个微调器。
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:spinnerMode="dropdown"
android:id="@+id/spn_college_names"></Spinner>
我使用Matrix Cursor和Simple Cursor Adapter设置它的值,如下所示 spn_CollegeNames =(Spinner)findViewById(R.id.spn_college_names);
String[] ColoumnNames = {"_id","CollegeName"};
_CollegeNames = getResources().getStringArray(R.array.settings_college_names);
MatrixCursor cursor = new MatrixCursor(ColoumnNames);
for (int CollegeIndex = 0; CollegeIndex < _CollegeNames.length; CollegeIndex++ )
{
cursor.addRow(new Object[] {CollegeIndex+1 , _CollegeNames[CollegeIndex]});
}
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cursor, new String[] {"CollegeName"}, new int[] {android.R.id.text1},0 );
/*TextView tv = (TextView) spn_CollegeNames.getSelectedView();
tv.setTextColor(Color.BLACK);*/
spn_CollegeNames.setAdapter(cursorAdapter);
我现在的问题是微调器中的文本显示为灰色。我想将文本更改为黑色。我知道我可以在布局中使用android:enteries属性但是我将来会用数据库光标替换矩阵光标,这意味着我没有固定的字符串数组值。
答案 0 :(得分:3)
您可以通过覆盖getDropdownView和/或getView来实现它。 getDropdownView返回展开的微调器的视图,getView返回折叠微调器的视图。
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cursor, new String[] {"CollegeName"}, new int[] {android.R.id.text1},0 ){
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position,convertView,parent);
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position,convertView,parent);
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}
};
答案 1 :(得分:1)
试试这个:
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
答案 2 :(得分:1)
而不是android.R.layout.simple_spinner_dropdown_item
,您必须提供custom textView
项目。
Spinnerlayout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00eeff"
android:text="TextView" />
您的适配器
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.spinnerlayout, cursor, new String[] {"CollegeName"}, new int[] {android.R.id.text1},0 );