我正在用古吉拉特语开发一个 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);
}