android spinner change font typeface

前端 未结 5 2064
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 14:23

i\'m going to change text typeface to arialbold in my spinner how i\'m going to do so. Below is my code :-

ArrayAdapter genderAdap = Arra         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 15:05

    // try this custom adapter for spinner
    class MyCustomAdapter extends ArrayAdapter {
    
            Context context;
            ArrayList list;
            private int defaultPosition;
    
            public int getDefaultPosition() {
                return defaultPosition;
            }
    
            public MyCustomAdapter(Context context, ArrayList objects) {
                super(context, 0, objects);
                this.context = context;
                list = objects;
            }
    
            public void setDefaultPostion(int position) {
                this.defaultPosition = position;
            }
    
            @Override
            public View getDropDownView(int position, View convertView,
                    ViewGroup parent) {
                return getCustomView(position, convertView, parent);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return getCustom(position, convertView, parent);
            }
    
            public View getCustom(int position, View convertView, ViewGroup parent) {
    
                View row = LayoutInflater.from(context).inflate(
                        android.R.layout.simple_spinner_item, parent, false);
                TextView label = (TextView) row.findViewById(android.R.id.text1);
                Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontsname");
                label.setTypeface(tf);
                label.setText(list.get(position));
    
                return row;
            }
    
            public View getCustomView(int position, View convertView,
                    ViewGroup parent) {
    
                View row = LayoutInflater.from(context).inflate(
                        android.R.layout.simple_spinner_item, parent, false);
                TextView label = (TextView) row.findViewById(android.R.id.text1);
                Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontname");
                label.setTypeface(tf);
                label.setText(list.get(position));
    
                return row;
            }
        }
    

提交回复
热议问题