修改控件Spinner内容的字体大小 颜色等属性

【修改控件Spinner内容的字体大小 颜色等属性】

  (2012-05-05 18:06:02)
转载
  分类: 移动开发之Android

 在anadroid开发中遇到此问题: 直接定义的Spinner控件,不能调整内容字体的大小,所以常会

因为控件太小,内容太长而使得不能完全显示,各种搜索,找到如下解决方法!希望对遇到同样问题

的朋友们提供帮助!

public class CustomSpinner extends Activity {

String[] numbers = { "One", "Two", "Three", "Four", "Five" };


@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    SpinnerAdapter adapter = new SpinnerAdapter(this,

            android.R.layout.simple_spinner_item, numbers);

    spinner.setAdapter(adapter);

}


private class SpinnerAdapter extends ArrayAdapter<String> {

    Context context;

    String[] items = new String[] {};


    public SpinnerAdapter(final Context context,

            final int textViewResourceId, final String[] objects) {

        super(context, textViewResourceId, objects);

        this.items = objects;

        this.context = context;

    }


    @Override

    public View getDropDownView(int position, View convertView,

            ViewGroup parent) {


        if (convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(context);

            convertView = inflater.inflate(

                    android.R.layout.simple_spinner_item, parent, false);

        }


        TextView tv = (TextView) convertView

                .findViewById(android.R.id.text1);

        tv.setText(items[position]);

        tv.setTextColor(Color.BLUE);

        tv.setTextSize(30);

        return convertView;

    }


    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(context);

            convertView = inflater.inflate(

                    android.R.layout.simple_spinner_item, parent, false);

        }


        // android.R.id.text1 is default text view in resource of the android.

        // android.R.layout.simple_spinner_item is default layout in resources of android.


        TextView tv = (TextView) convertView

                .findViewById(android.R.id.text1);

        tv.setText(items[position]);

        tv.setTextColor(Color.BLUE);

        tv.setTextSize(30);

        return convertView;

    }

}

你可能感兴趣的:(修改控件Spinner内容的字体大小 颜色等属性)