Android修改系统自带Spinner字体大小和颜色的完整示例

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 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 {
  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如下:




    





数据:

   
        
        失眠
        贫血
        脱发
        疲惫
        烦躁
        偏胖
        偏瘦
        白发
        头痛
        近视
        弱视
        耳鸣
    
   

你可能感兴趣的:(Android修改系统自带Spinner字体大小和颜色的完整示例)