Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android修改系統自帶Spinner字體大小和顏色的完整示例

Android修改系統自帶Spinner字體大小和顏色的完整示例

編輯:關於Android編程

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




    

arrays.xml如下:




    
        
        失眠
        貧血
        脫發
        疲憊
        煩躁
        偏胖
        偏瘦
        白發
        頭痛
        近視
        弱視
        耳鳴
    
   



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved