如何将不同的字体样式应用于微调器中的不同项?

时间:2014-04-04 05:34:20

标签: android fonts android-spinner

在我的应用程序中,我有一个旋转器,显示星期日,星期一,星期二,星期四,星期五,星期六。我想以不同的字体样式显示每个项目。星期日以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>

1 个答案:

答案 0 :(得分:2)

在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语句