我正在开发一款古吉拉特语的Android应用程序,需要在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;
}
}
当我在模拟器上尝试时,会得到框。但是当我在我的平板电脑上尝试时,什么也没有出现。
任何帮助将不胜感激。
编辑: 这个有效:感谢此人。
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);
}