如何更改微调器文本大小?

发布于 2024-10-21 09:48:39 字数 36 浏览 7 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-10-28 09:48:39

在 Android 中,Spinner 只是一个组合框或列表框。

它允许您查看多个项目并允许您从列表中选择一项。

像这样编辑您的 XML 代码

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

您的 Java 类代码应如下所示

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

输出将如下所示

在此处输入图像描述

该网站提供带有源代码的示例屏幕截图
http://www.androidpeople.com/android-spinner-example

一般来说我们可以不通过简单的适配器编辑文本大小或文本颜色,在第一个xml文件中,我们声明通过spinnername.findviewbyid(id)找到的旋转器和第一个java文件。我们只是通过xml文件创建自定义适配器,即首先我们创建第二个xml文件,在其中给出我们的要求,如文本视图,图像等,在文本视图中,我们给出文本颜色和文本大小,然后我们在java中创建自定义适配器文件,我们只是通过自定义适配器中的布局膨胀器来膨胀该xml文件,最后我们在微调器中传递该适配器。您的自定义查看微调器被创建。

自定义视图的示例,您可以在其中设置文本大小、文本颜色和图像以及许多其他内容:-

在此创建联系人列表,并使用自定义适配器将以下 xml 文件填充到
contactadapter 文件

xml 文件:-

<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"/>

自定义适配器文件:-

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;

}

}

我们假设您创建了定义 spinner 的firstxml 文件,最后在firstjava 文件中,您只需添加我们传递自定义适配器的 spinner 代码:

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

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);
烟雨凡馨 2024-10-28 09:48:39

hello-spinner 教程非常有用。

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

将新的 XML 文件添加到布局文件夹中。

安卓:textColor =“#FF8B1500”
android:gravity="center"/>

将适配器资源更改为新的布局文件:

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

`

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

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文