2013-10-03 97 views
3

我要將文本字體更改爲arialbold在我的微調器中,我該如何去做。下面是我的代碼: -android spinner更改字體字體

ArrayAdapter<CharSequence> genderAdap = ArrayAdapter.createFromResource(getActivity(), R.array.gender,R.layout.spinner_item); 


genderAdap.setDropDownViewResource(R.layout.spinner_item); 

ddlGender.setAdapter(genderAdap); 

回答

9

類:

public class testActivity extends Activity { 

    private static final String[] COUNTRIES = new String[] { "Belgium", 
      "France", "Italy", "Germany", "Spain" }; 
    private Spinner mySpinner; 
    private Typeface myFont; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.newlay); 

     mySpinner = (Spinner) findViewById(R.id.spinner1); 
     myFont = Typeface.createFromAsset(getAssets(), "gujarti.ttf"); 
     MyArrayAdapter ma = new MyArrayAdapter(this); 
     mySpinner.setAdapter(ma); 
    } 

    private class MyArrayAdapter extends BaseAdapter { 

     private LayoutInflater mInflater; 

     public MyArrayAdapter(testActivity con) { 
      // TODO Auto-generated constructor stub 
      mInflater = LayoutInflater.from(con); 
     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return COUNTRIES.length; 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      final ListContent holder; 
      View v = convertView; 
      if (v == null) { 
       v = mInflater.inflate(R.layout.my_spinner_style, null); 
       holder = new ListContent(); 

       holder.name = (TextView) v.findViewById(R.id.textView1); 

       v.setTag(holder); 
      } else { 

       holder = (ListContent) v.getTag(); 
      } 

      holder.name.setTypeface(myFont); 
      holder.name.setText("" + COUNTRIES[position]); 

      return v; 
     } 

    } 

    static class ListContent { 

     TextView name; 

    } 
} 

佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

my_spinner_style.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout> 

TTF FILES

+0

我嘗試這仍然沒有工作。在代碼後添加'adapter.setDropDownViewResource(R.layout.spinner_item); \t \t ddlGender.setAdapter(adapter);' – nick

+0

可以告訴我錯誤嗎? – KOTIOS

+0

沒有錯誤只是微調空數據 – nick

0

對於改變的微調項目的字體,你必須使用定製微調

嘗試以設置在旋轉所有的TextView的自定義字體:

Typeface typeface; 
font_name_Adapter= new ArrayAdapter<String> 
       (this,android.R.layout.simple_spinner_item,ARRLIST_FONTS) 
     { 
      public View getView(int position, View convertView,ViewGroup parent) { 
       View v = super.getView(position, convertView, parent); 
       ((TextView) v).setTextSize(12); 
       ((TextView) v).setTextColor(Color.WHITE); 

       if(position<=ARRLIST_FONTS.size()){ 
       typeface = 
        Typeface.createFromAsset(Your_Current_Activity.this.getAssets(), 
           "fonts/yourfontname.ttf"); 
        ((TextView) v).setTypeface(typeface); 
        } 

        return v; 
    } 
+0

我試過不工作可以ü定製基於我的示例代碼? – nick

+1

@nick看到這個鏈接自定義適配器微調http://stackoverflow.com/questions/9970675/example-of-custom-setdropdownviewresource-spinner-item http://karanbalkar.com/2013/07/tutorial- 39-create-custom-spinner-in-android/ – Andrain

0

您將通過自己的自定義Spinne應用字體rAdapter,在getView()和getDropDownView()

public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = getLayoutInflater(); 
      View row = inflater.inflate(yourRowlayout, parent, 
        false); 
     TextView make = (TextView) row.findViewById(R.id.Make); 
     Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), 
       "fonts/gilsanslight.otf"); 
     v.setTypeface(myTypeFace); 
     v.setText(itemList.get(position)); 
     return row; 
    } 


public View getDropDownView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = getLayoutInflater(); 
       View row = inflater.inflate(yourRowlayout, parent, 
         false); 
      TextView make = (TextView) row.findViewById(R.id.Make); 
      Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), 
        "fonts/gilsanslight.otf"); 
      v.setTypeface(myTypeFace); 
      v.setText(itemList.get(position)); 
      return row; 
     } 
2

這裏是你如何能做到這一點,設置您的適配器如下:

mMsgAdap =new ArrayAdapter<CharSequence>(getApplicationContext(), R.layout.spinner_item_list, mCategories) 
       { 

        public View getView(int position, View convertView, ViewGroup parent) 
         { 
          View v = super.getView(position, convertView, parent); 
          ((TextView) v).setTypeface(StaticUtils.sTypeFace(getApplicationContext()));//Typeface for normal view 

          return v; 
         } 
        public View getDropDownView(int position, View convertView, ViewGroup parent) 
         { 
          View v = super.getDropDownView(position, convertView, parent); 
          ((TextView) v).setTypeface(StaticUtils.sTypeFace(getApplicationContext()));//Typeface for dropdown view 
          ((TextView) v).setBackgroundColor(Color.parseColor("#BBfef3da")); 
          return v; 
         } 
       }; 
     mMsgAdap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     mMsgSpnr.setAdapter(mMsgAdap); 
     mMsgSpnr.setSelection(0); 

和字體方法我已經採取靜態StaticUtils類內部的我創建是

public static Typeface sTypeFace(Context mCnxt) { 
    Typeface mtypeface = Typeface.createFromAsset(mCnxt.getAssets(), 
      "fonts/forte-mt-1361539051.ttf"); 
    return mtypeface; 
} 
+0

我試着用同樣的數據不填充。任何意見? – nick

+0

@nick你在哪裏存儲你的數據? – 2013-10-03 04:47:24

+0

在R.array.country。我將mCategories更改爲R.array.country – nick

0
// try this custom adapter for spinner 
class MyCustomAdapter extends ArrayAdapter<String> { 

     Context context; 
     ArrayList<String> list; 
     private int defaultPosition; 

     public int getDefaultPosition() { 
      return defaultPosition; 
     } 

     public MyCustomAdapter(Context context, ArrayList<String> objects) { 
      super(context, 0, objects); 
      this.context = context; 
      list = objects; 
     } 

     public void setDefaultPostion(int position) { 
      this.defaultPosition = position; 
     } 

     @Override 
     public View getDropDownView(int position, View convertView, 
       ViewGroup parent) { 
      return getCustomView(position, convertView, parent); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      return getCustom(position, convertView, parent); 
     } 

     public View getCustom(int position, View convertView, ViewGroup parent) { 

      View row = LayoutInflater.from(context).inflate(
        android.R.layout.simple_spinner_item, parent, false); 
      TextView label = (TextView) row.findViewById(android.R.id.text1); 
      Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontsname"); 
      label.setTypeface(tf); 
      label.setText(list.get(position)); 

      return row; 
     } 

     public View getCustomView(int position, View convertView, 
       ViewGroup parent) { 

      View row = LayoutInflater.from(context).inflate(
        android.R.layout.simple_spinner_item, parent, false); 
      TextView label = (TextView) row.findViewById(android.R.id.text1); 
      Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontname"); 
      label.setTypeface(tf); 
      label.setText(list.get(position)); 

      return row; 
     } 
    }