如何更改微调文字的大小?如何更改、大小、文字

2023-09-13 01:44:53 作者:骂过三八也爱过人渣

我想了解微调以及如何更改微调文字的大小和微调文本颜色。

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

推荐答案

在Android的,微调只不过是一个组合框或列表框。

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.

编辑XML code这样

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

Java类code应该是这样

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

的输出看起来像

本网站提供的样品截屏与源$ C ​​$ C http://www.androidpeople.com/android-spinner-example

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

Generaly我们不能编辑通过简单的适配器TEXTSIZE或文本颜色,在firstxml文件中我们声明,我们发现通过spinnername.findviewbyid(ID)的微调和firstjava文件。我们只需要创建通过XML文件中的自定义适配器即首先我们创建secondxml文件中,我们给出了类似的TextView我们的要求,图像等,在TextView中我们给出了文本颜色和TEXTSIZE那么我们在java中customadapterfile创造,我们只是通过膨胀充气布局的XML文件中,我们的自定义适配器,最后我们通过在该适配器spinner.Your定制观看微调被创建。

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.

例如对于您设置TEXTSIZE,文字颜色和图像也和许多事情的自定义视图: -

在此联系人列表是由和使用自定义适配器,我们在下面打气XML文件中 contactadapter文件

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

XML文件: -

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;

}

}

我们假设你创造一个微调定义,终于在firstjava文件,你刚才添加的code的微调,我们通过自定义适配器firstxml文件:

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);