【问题标题】:How to apply different font styles to difeerent items in the spinner?如何将不同的字体样式应用于微调器中的不同项目?
【发布时间】:2014-04-04 05:34:19
【问题描述】:

在我的应用程序中,我有一个微调器,它显示星期日、星期一、星期二、星期四、星期五、星期六。我想以不同的字体样式显示每个项目。即星期日以 Times New Roman 字体显示,星期二以 Calibri 字体显示,星期三在像这样的 Arialic Hollow 字体中。我将这些字体放在资产中创建的字体文件夹中。请帮助我怎么做。

我的主要活动是

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
 public class AndroidCustomSpinner extends Activity{

    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", 
            "Wednesday", "Thursday", "Friday", "Saturday"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                R.layout.row, R.id.weekofday, DayOfWeek);
        mySpinner.setAdapter(adapter); 
    }

}

我的 row.xml 是

<?xml version="1.0" encoding="utf-8" ?> 
 <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal">
<ImageView 
  android:id="@+id/icon" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:src="@drawable/ic_launcher" /> 
<TextView 
  android:id="@+id/weekofday" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" /> 
 </LinearLayout>

我的 main.xml 是

 <?xml version="1.0" encoding="utf-8" ?> 
 <LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">
 <TextView 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="Spinner" /> 
 <Spinner 
  android:id="@+id/spinner" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" /> 
 </LinearLayout>

【问题讨论】:

  • 编写一个 customAdapter 并在 getView 和 getDropDownView 方法中根据您的要求更改 textview 的字体..
  • 就像 Microsoft Word 中的字体下拉菜单一样吗?
  • ya 跟ms word出现的方式一样
  • 这里是 listview 上每一行的样式示例。 stackoverflow.com/a/13157347/1752867
  • 我想更改每个项目的字体。我知道如何更改所有微调器项目的字体。但我需要单独更改每个微调器项目。我在这里使用布局。

标签: android fonts android-spinner


【解决方案1】:

在AdapterArray中使用这个来格式化

public View getCustomView(int position, View convertView, ViewGroup parent) 
{
    //inflate the view
    View row = inflater.inflate(R.layout.spinner_rows, parent, false);
    TextView dayView=(TextView)row.findViewById(R.id.dayView);//view where you show days
    if(position==1)//if 1 is for sunday
    {
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/TimesNewRoman.ttf"); 
        dayView.setTypeface(face); 
    }
    else if(position==2)//if 1 is for monday
    {
        Typeface face=Typeface.createFromAsset(getAssets(), "fonts/someotherFont.ttf"); 
        dayView.setTypeface(face); 

    }
   return row;
}

您可以制作天数和格式的数组,并在函数中迭代它们而不是 if else 语句

【讨论】:

  • 请告诉我如何更改星期天,然后我会继续
  • 这很好。但是在我的程序中我应该如何使用它。我不明白。
  • 创建您自己的自定义 arrayadatper 并覆盖答案中的 getCustomView,现在创建自定义adatper 的实例并将其设置为微调器,请参考教程或逐步过程,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
相关资源
最近更新 更多