Set the TextSize to a text in spinner in android programatically

后端 未结 4 1600
醉话见心
醉话见心 2021-01-03 01:27

Hi I want to set the style and size for a text in spinner programatically(Dynamically).I don\'t use any resources in my app for this.So give me some suggestions for this

4条回答
  •  独厮守ぢ
    2021-01-03 02:27

    I dont think you can create this dynamically without overriding the behavior of the default layout resources . To create it using resources:

    Create a layout file containing a TextView and define the size,colors and other style for this. And create an ArrayAdapter object and provide that layout file in your adapter alongwith the ID of the TextView.

    Your layout file will be like this: spinner_item.xml

    
    
    
    
    

    Now you can use this in your code like this:

    Spinner mySpinner = (Spinner)findViewById(R.id.spinner);      
    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_item,R.id.textview,Your_Array_of_items);      
    mySpinner.setAdapter(adapter); 
    

    Also you can create a custom ArrayAdapter and overriding the methods

    getView() or getDropDownView()

    inside this methods you can set custom color, size and fonts for your TextView

    Update:

    I have changed the text size and color of the spinner items dynamically by overrding the default resources of android. The snippet I have used is as given below:

    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 {
        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;
        }
    }
    

提交回复
热议问题