How to change spinner text size?

后端 未结 2 1611
深忆病人
深忆病人 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

    <Spinner android:id="@+id/Spinner01"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    

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

    <TextView android:text="Name:" android:id="@+id/tvNameCustomContact"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginLeft="10dip" android:textColor="@color/darkcherryred" 
        />
    <TextView android:id="@+id/tvNumberCustomContact" android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
         android:text="Number:" android:textColor="@color/DarkGrey" android:paddingLeft="10dip"
         android:layout_below="@+id/tvNameCustomContact" 
         />
    <TextView android:text="Group:" android:id="@+id/tvGroupCustomContact"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
         android:textColor="@color/darkcherryred"   android:paddingLeft="10dip"
         android:layout_below="@+id/tvNumberCustomContact"/>
    

    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<String> name=new ArrayList<String>();
    private ArrayList<String> number=new ArrayList<String>();
    private ArrayList<String> group=new ArrayList<String>();
    
    private LayoutInflater mInflater;
    public ContactAdapter(Context context,  ArrayList<String> name,ArrayList<String> number,ArrayList<String> 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);
    
    0 讨论(0)
  • 2021-01-15 06:50

    The hello-spinner tutorial is very useful.

    http://developer.android.com/guide/tutorials/views/hello-spinner.html

    Add a new XML file to your layout folder.

    android:textColor="#FF8B1500" android:gravity="center"/>

    Change the adapter resource to your new layout file:

    adapter = ArrayAdapter.createFromResource(
                this, R.array.sound, R.layout.spinnerLayout);
    

    `

    0 讨论(0)
提交回复
热议问题