[英]How do you use a custom font on Strings that are being used in a RecyclerView?
I have a very simple RecyclerView but I want to put a custom font on the Strings that are being passed to the View. 我有一个非常简单的RecyclerView,但我想在传递给View的Strings上放一个自定义字体。 I have a few extra things but you could just ignore that code.
我还有一些其他事情,但是您可以忽略该代码。 Here is my adapter :
这是我的适配器:
public class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
private List<ListItem> ListData;
private LayoutInflater inflater;
private ItemClickCallBack itemClickCallBack;
public interface ItemClickCallBack {
void onItemClick(int p);
void onSecondaryClick(int p);
}
public void setItemClickCallBack(final ItemClickCallBack itemClickCallBack){
this.itemClickCallBack= itemClickCallBack;
}
public Adapter(List<ListItem> ListData, Context context) {
inflater = LayoutInflater.from(context);
this.ListData = ListData;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
ListItem item = ListData.get(position);
holder.title.setText(item.getTitle());
holder.subtitle.setText(item.getSubTitle());
if(item.isStar()){
holder.thumbnail.setImageResource(R.drawable.ic_star_black_24dp);
}else{
holder.thumbnail.setImageResource(R.drawable.ic_star_border_black_24dp);
}
}
@Override
public int getItemCount() {
return ListData.size();
}
public void setListData (ArrayList<ListItem> exerciseList){
this.ListData.clear();
this.ListData.addAll(exerciseList);
}
class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title;
private View container;
private TextView subtitle;
private ImageView avatar;
private ImageView thumbnail;
public Holder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.textRoot);
container = itemView.findViewById(R.id.listItem);
container.setOnClickListener(this);
subtitle = (TextView) itemView.findViewById(R.id.txt_subtitle);
thumbnail = (ImageView) itemView.findViewById(R.id.secondaryIcon);
thumbnail.setOnClickListener(this);
avatar = (ImageView) itemView.findViewById(R.id.img_avatar);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.listItem){
itemClickCallBack.onItemClick(getAdapterPosition());
}else{
itemClickCallBack.onSecondaryClick(getAdapterPosition());
}
}
}
} }
This is where my data is saved. 这是保存我的数据的地方。 I know that I am running the same thing a couple times, but I was just seeing the scrolling.
我知道我在同一时间运行了几次,但是我只是在看到滚动。 Anyways, here is the code:
无论如何,这是代码:
public class Data {
final private static String duas[] = {"Hello to you","Bye to you","Walaa"};
final private static String Trans[] = {"Hmmmm", "Bruh","Khalas"};
private final static int face = R.drawable.ic_tag_faces_black_24dp;
public static List<ListItem> getListData() {
List<ListItem> data = new ArrayList<>();
for (int x = 0; x < 4; x++) {
for (int i = 0; i < duas.length; i++) {
ListItem item = new ListItem();
item.setTitle(duas[i]);
item.setSubTitle(Trans[i]);
data.add(item);
}
}
return data;
}
} }
And if it is not possible to put a custom font on a String, what would I have to do to make all my data a TextView? 而且,如果无法在String上放置自定义字体,我该怎么做才能使所有数据成为TextView?
Thank you, 谢谢,
Shahrukh Raza 沙鲁克·拉扎(Shahrukh Raza)
You'll need a custom TextView to use custom font. 您需要一个自定义TextView才能使用自定义字体。
Please refer to this link: How to make a custom TextView? 请参考此链接: 如何制作自定义TextView?
You should create an assets folder, then create a sub-folder fonts and put all your fonts into there. 您应该创建一个资源文件夹,然后创建一个子文件夹字体,然后将所有字体放入其中。
In your Holder class: 在您的Holder类中:
TextView title = (TextView) itemView.findViewById(R.id.textRoot);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/your_font_name.ttf");
title.setTypeface(face);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.