How to change spinner text size?

后端 未结 2 1613
深忆病人
深忆病人 2021-01-15 06:27

I\'d like to learn about spinner and how to change spinner text size and spinner text color.

2条回答
  •  粉色の甜心
    2021-01-15 06:48

    In Android, Spinner is nothing but a combo box or list box.

    It lets you viewing multiple items and allows you to select one item from the list.

    Edit Your XML code like this

    
    

    Your Java Class code should look like this

    public class SpinnerExample extends Activity { 
       private String array_spinner[];
       @Override
       public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         array_spinner=new String[5];
         array_spinner[0]="1";
         array_spinner[1]="2";
         array_spinner[2]="3";
         array_spinner[3]="4";
         array_spinner[4]="5";
         Spinner s = (Spinner) findViewById(R.id.Spinner01);
         ArrayAdapter adapter = new ArrayAdapter(this,
         android.R.layout.simple_spinner_item, array_spinner);
         s.setAdapter(adapter);
       }
     }
    

    The Output will look like

    enter image description here

    This site gives sample screen shot with source code
    http://www.androidpeople.com/android-spinner-example

    Generaly we can't edit the textsize or textcolor through simple adapter,in firstxml file we declare the spinner and firstjava file we find through spinnername.findviewbyid(id).we just create the custom adapter through xml file i.e firstly we create secondxml file in which we gives our requirements like textview,images etc. ,in textview we gives the textcolor and textsize then we create customadapterfile in java and we just inflate that xml file through layout inflater in our custom adapter and finally we pass that adapter in spinner.Your custom viewing spinner is created.

    example for custom view where you set the textsize,textcolor and images also and many thing:-

    In this a contact list is made and using custom adapter we inflate below xml file in contactadapter file

    xml file :-

    
    
    
    

    custom adapter file:-

    import java.util.ArrayList;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageButton;
    import android.widget.TextView;
    
    public class ContactAdapter extends BaseAdapter 
    {
    
    
    private ArrayList name=new ArrayList();
    private ArrayList number=new ArrayList();
    private ArrayList group=new ArrayList();
    
    private LayoutInflater mInflater;
    public ContactAdapter(Context context,  ArrayList name,ArrayList number,ArrayList group1) 
    {
        this.mInflater = LayoutInflater.from(context);
        this.name=name;
        this.number=number;
        this.group=group1;
    }
    
    public int getCount() {
        return this.name.size();
    }
    
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        final ViewHolder holder;
        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.contactcustomlist, null);
            holder = new ViewHolder();
            holder.Name = (TextView) convertView.findViewById(R.id.tvNameCustomContact);
            holder.Number= (TextView) convertView.findViewById(R.id.tvNumberCustomContact);
            holder.Group= (TextView) convertView.findViewById(R.id.tvGroupCustomContact);
            convertView.setTag(holder);
        } 
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }
    
    
    
    
    
        holder.Name.setText    ("Name :      "+name.get(position));
        holder.Number.setText("Numbers : "+number.get(position));
        holder.Group.setText   ("Group :      "+group.get(position));
    
    
        return convertView;
    
    }
    class ViewHolder {
        TextView Name;
        TextView Number;
        TextView Group;
    
    }
    

    }

    we assume that you create firstxml file in which spinner is defined,finally in firstjava file you just add the code for spinner where we pass the custom adapter:

    ContactAdapter contactadapter = new ContactAdapter(this, NameA, MobileA, group);//NameA,MobileA,Group is a arraylist in which we pass the values from main java file to ContactAdapter java file
    Spinner spinner= (Spinner)findviewbyid(R.id.spinnername);
    spinner.setAdapter(contactadapter);
    

提交回复
热议问题