在Android中的Spinner上设置Gujarati文本

时间:2013-01-30 10:37:35

标签: android fonts spinner

我正在使用古吉拉特语开发Android应用,需要在Spinner小部件上设置古吉拉特语文本。所以,我学会了如何从HERE.

在Spinner上设置自定义字体

但问题是我得到的是盒子而不是实际文本。我的spinner文本来自字符串资源。附上我的代码。

public class SpinnerExActivity extends Activity {
Spinner mySpinner; 
Typeface myFont;

String[] items; 
MyArrayAdapter ma;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex);
    mySpinner = (Spinner) findViewById(R.id.spinner1);
     items=getResources().getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
     ma=new MyArrayAdapter(this, R.layout.my_spinner_style); 
     ma.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


}
private class MyArrayAdapter extends ArrayAdapter {

    public MyArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    }

    public TextView getView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    v.setTypeface(myFont);
    v.setText(items[position]);
    return v;
    }

    public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    v.setTypeface(myFont);
    return v;
    }

    }

我在模拟器上尝试这个时会收到盒子。但是当我在平板电脑上试用它时我什么都没得到。

任何帮助都会很明显。

修改 这个Works :: Thanx到this.

public class SpinnerExActivity extends Activity {
Spinner mySpinner; 
Typeface myFont;

String[] items; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex);
    mySpinner = (Spinner) findViewById(R.id.spinner1);
     items=getResources().getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             R.layout.my_spinner_style, items) {

  public View getView(int position, View convertView, ViewGroup parent) {
          View v = super.getView(position, convertView, parent);

          Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
          ((TextView) v).setTypeface(externalFont);
          ((TextView) v).setText(items[position]);
          ((TextView) v).setTextColor(Color.BLACK);
          return v;
  }


  public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
           View v =super.getDropDownView(position, convertView, parent);

          Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
          ((TextView) v).setTypeface(externalFont);
          ((TextView) v).setText(items[position]);

          return v;
   }
   };


     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                                 
     mySpinner.setAdapter(adapter);

}

1 个答案:

答案 0 :(得分:2)

你必须注意这个:

enter image description here

检查以下代码:

package com.example.main.util;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.testapp.R;

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;

    }
}

祝你好运!