2014-04-04 26 views
0

在我的應用程序,我有一個微調,顯示星期日,星期一,星期二,星期四,星期五,星期六。我想顯示每個項目在不同的字體style.ie週日在泰晤士報新羅馬字體,星期二在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> 
+0

寫的customAdapter和getView迭代和getDropDownView方法改變的TextView的字體根據您的需要.. –

+0

就像一個字體在Microsoft Word中下拉右鍵? – Kedarnath

+0

亞以ms字出現的相同方式 – Lakshmipathi

回答

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

你可以做的日期和格式的數組,並通過他們的功能,而不是如果else語句

+0

請給我一個想法如何改變星期日然後我將繼續 – Lakshmipathi

+0

你好,我剛剛編輯答案,這有幫助嗎? –

+0

這很好。但在我的程序中,我該如何使用它。我沒有那麼做。 – Lakshmipathi

相關問題