您好我正在尝试为 TextView 设置自定义字体,但由于无法从 contextWrapper 对非静态方法 getAssets() 进行静态引用,因此出现错误。如何设置自定义字体?. 我的代码如下。有没有办法在 XML 布局文件本身中设置字体。它存在于资产中。
public class MainActivity extends Activity {
private static class EfficientAdapter extends BaseAdapter {
Typeface tfTab = Typeface.createFromAsset(getAssets(),"fonts/tabkovai.ttf");
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rowlayout, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
return convertView;
}
static class ViewHolder {
TextView text;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.list);
lv.setFocusable(true);
lv.setFastScrollEnabled(true);
lv.setAdapter(new EfficientAdapter(this));
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(getApplicationContext(), "Hello : " + position, Toast.LENGTH_SHORT).show();
}
});
}
private static final String[] DATA = {"Üô¢ ð£î¢î¤ý£ - «î£ø¢Áõ£ò¢"};// Lot of strings
}