我正在使用RecyclerView
来显示三个不同的文字,我想为每个文本应用自定义Typeface
。现在在ViewHolder
下的我的适配器中,我放了Typeface
代码,但不知道如何将它们应用到每个textView
。
Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
List<AdapterData> mItems;
public Adapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
mItems.add(data);
data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
mItems.add(data);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cardview_items, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
AdapterData data = mItems.get(i);
viewHolder.textOne.setText(data.getTextOne());
viewHolder.textTwo.setText(data.getTextTwo());
viewHolder.textThree.setText(data.getTextThree());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textOne;
public TextView textTwo;
public TextView textThree;
Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");
public ViewHolder(View itemView) {
super(itemView);
textOne = (TextView)itemView.findViewById(R.id.textView1);
textTwo = (TextView)itemView.findViewById(R.id.textView2);
textThree = (TextView)itemView.findViewById(R.id.textView3);
}
}
}
答案 0 :(得分:2)
class ViewHolder extends RecyclerView.ViewHolder {
public TextView textOne;
public TextView textTwo;
public TextView textThree;
Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");
public ViewHolder(View itemView) {
super(itemView);
textOne = (TextView) itemView.findViewById(R.id.textView1);
textOne.setTypeface(customTypeOne);
textTwo = (TextView) itemView.findViewById(R.id.textView2);
textTwo.setTypeface(customTypeTwo);
textThree = (TextView) itemView.findViewById(R.id.textView3);
textThree.setTypeface(customTypeThree);
}
}
答案 1 :(得分:0)
您实际上只需将字体添加到textview:
public ViewHolder(View itemView) {
super(itemView);
...
textOne.setTypeface(customTypeOne)
...
答案 2 :(得分:0)
您可以使用setTypeface()方法在yout textview上设置创建的字体:
textOne.setTypeface(customTypeOne);
textTwo.setTypeface(customTypeTwo);
textThree.setTypeface(customTypeThree);
我还建议你在github上看书法。它是一个有效处理字体困境的强大库:)
答案 3 :(得分:0)
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
viewHolder.textOne.setTypeface(myTypeface);
viewHolder.textOne.setText(data.getTextOne());
答案 4 :(得分:0)
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
List<AdapterData> mItems;
public Adapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
mItems.add(data);
data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
data = new AdapterData();
data.setTextOne("Title 1");
data.setTextTwo("Title 2");
data.setTextThree("Title 3");
mItems.add(data);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cardview_items, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
AdapterData data = mItems.get(i);
viewHolder.textOne.setText(data.getTextOne());
viewHolder.textTwo.setText(data.getTextTwo());
viewHolder.textThree.setText(data.getTextThree());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textOne;
public TextView textTwo;
public TextView textThree;
Typeface customTypeOne = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface One.ttf");
Typeface customTypeTwo = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Two.ttf");
Typeface customTypeThree = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Typeface Three.ttf");
public ViewHolder(View itemView) {
super(itemView);
textOne = (TextView)itemView.findViewById(R.id.textView1);
textTwo = (TextView)itemView.findViewById(R.id.textView2);
textThree = (TextView)itemView.findViewById(R.id.textView3);
textOne.setTypeface(customTypeOne);
textTwo.setTypeface(customTypeTwo);
textThree.setTypeface(customTypeThree);
}
}
}