MainActivity如下:
package cc.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
/**
* Demo描述:
* 修改系统自带Spinner字体大小和颜色的完整示例
*
* 参考资料:
* http://blog.csdn.net/xuzhuang2008/article/details/7641685
* Thank you very much
*/
public class TestSpinnerActivity extends Activity {
private Spinner mSpinner;
private ArrayAdapter<String> mAdapter ;
private String [] mStringArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mSpinner=(Spinner) findViewById(R.id.spinner);
mStringArray=getResources().getStringArray(R.array.test_string_array);
//使用自定义的ArrayAdapter
mAdapter = new TestArrayAdapter(TestSpinnerActivity.this,mStringArray);
//设置下拉列表风格(这句不些也行)
//mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(mAdapter);
//监听Item选中事件
mSpinner.setOnItemSelectedListener(new ItemSelectedListenerImpl());
}
private class ItemSelectedListenerImpl implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long arg3) {
System.out.println("选中了:"+mStringArray[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
}
}
TestArrayAdapter如下:
package cc.test;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class TestArrayAdapter extends ArrayAdapter<String> {
private Context mContext;
private String [] mStringArray;
public TestArrayAdapter(Context context, String[] stringArray) {
super(context, android.R.layout.simple_spinner_item, stringArray);
mContext = context;
mStringArray=stringArray;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
//修改Spinner展开后的字体颜色
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent,false);
}
//此处text1是Spinner默认的用来显示文字的TextView
TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
tv.setText(mStringArray[position]);
tv.setTextSize(22f);
tv.setTextColor(Color.RED);
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 修改Spinner选择后结果的字体颜色
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
}
//此处text1是Spinner默认的用来显示文字的TextView
TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
tv.setText(mStringArray[position]);
tv.setTextSize(18f);
tv.setTextColor(Color.BLUE);
return convertView;
}
}
main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
数据:
<string-array name="test_string_array">
<item>无</item>
<item>失眠</item>
<item>贫血</item>
<item>脱发</item>
<item>疲惫</item>
<item>烦躁</item>
<item>偏胖</item>
<item>偏瘦</item>
<item>白发</item>
<item>头痛</item>
<item>近视</item>
<item>弱视</item>
<item>耳鸣</item>
</string-array>
</resources>