我有一个加载RecyclerView的碎片,但我想在RecyclerView的其中一个项目中的TextView中添加自定义字体。在适配器的哪个位置应该设置字体?另外,如何设置字体?
我不能使用这段代码,
Typeface font = Typeface.createFromAsset((getAssets(),
"myfont.ttf");
因为我不在Activity中。那么我该如何获取字体?
这是我的适配器,
public class DiscoverRecyclerAdapter
extends RecyclerView.Adapter<DiscoverRecyclerAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mText;
public ViewHolder(android.support.v7.widget.CardView v) {
super(v);
mText = (TextView) v.findViewById(R.id.text);
}
}
public DiscoverRecyclerAdapter(String[] myDataset) {
mDataset = myDataset;
}
@Override
public DiscoverRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_discover, parent, false);
ViewHolder vh = new ViewHolder((android.support.v7.widget.CardView)v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mText.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
我应该在适配器的哪里设置字体,如何设置字体?