为eche RecyclerView项目设置自定义字体

时间:2018-01-19 19:00:36

标签: java android fonts android-fonts

我使用ArraysArrays.toString(),我想在其中一个RecyclerView项目中为CardView添加自定义字体。

  1. 我的适配器应该在哪里设置字体?
  2. 我究竟该如何设置字体?
  3. 例如:在TextView中设置 Durwent.ttf ,在RecyclerView中设置“Fenwick.ttf”。

    这是我的item 1

    item 2

3 个答案:

答案 0 :(得分:0)

Android Docs about Fonts

将android-family设置为Android doc中的描述。

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/lobster_regular" />
<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/lobster_italic" />
</font-family>

将font-family应用于TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/lobster"/>

创建具有两个不同项目的回收站视图,例如, FontViewHolder(在此CardView中包含支持字体的TextView)&amp; NormalViewHolder并根据条件,您可以使用特定的ViewHolder。例如,

public class YourAdapater extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private static final int FONT_ITEM_TYPE = 0x01;
  private static final int DEFAULT_ITEM_TYPE = 0x02;

  private List<RecyclerItem> listItems;

  public YourAdapater(List<RecyclerItem> listItems) {
    this.listItems = listItems;
  }

  @Override
  public int getItemCount() {
    return listItems.size();
  }

  @Override
  public int getItemViewType(int position) {
    if (position == 0) {
      return FONT_ITEM_TYPE; // constant say 0x01
    }
    return DEFAULT_ITEM_TYPE; // constant say 0x02
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    if (viewType == FONT_ITEM_TYPE) {
      // inflate and return FontViewHolder
      final View view = inflater.inflate(R.layout.recycler_item_font, parent, false);
      return new FontViewHolder(view);
    } else {
      // inflate and return NormalViewHolder
      final View view = inflater.inflate(R.layout.recycler_item_normal, parent, false);
      return new NormalViewHolder(view);
    }
  }

  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
    if (position == 0) {
      FontViewHolder vh = (FontViewHolder) holder;
      vh.bind(listItems.get(position));
    } else {
      NormalViewHolder vh = (NormalViewHolder) holder;
      vh.bind(listItems.get(position));
    }
  }

  public static class NormalViewHolder extends RecyclerView.ViewHolder {

    public TextView txtTitle;
    public TextView txtDescription;
    public TextView txtOptionDigit;

    private RecyclerItem item;

    public NormalViewHolder(View itemView) {
      super(itemView);

      txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
      txtDescription = (TextView) itemView.findViewById(R.id.txtDescription);
      txtOptionDigit = (TextView) itemView.findViewById(R.id.txtOptionDigit);

      itemView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          // click action on item
          if (item != null) {
            Toast.makeText(itemView.getContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
          }
        }
      });
    }

    public void bind(RecyclerItem item) {
      this.item = item;
      // do data binding
      txtTitle.setText(item.getTitle());
      txtDescription.setText(item.getDescription());
    }
  }

  public static class FontViewHolder extends RecyclerView.ViewHolder {

    public FontViewHolder(View itemView) {
      super(itemView);

      itemView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          // click action on item
        }
      });
    }

    public void bind(RecyclerItem data) {
      // do data binding
    }
  }
} 

通过这种方式,您可以在将来重新使用font-family并根据您的上下文扩展功能。

答案 1 :(得分:0)

@ M.bala7请在下面的步骤中设置不同位置的不同字体

1-首先创建资产文件夹    右键点击应用&gt;新&gt;文件夹&gt; assest文件夹&gt;选择主要&gt;点击确定

enter image description here 2-将字体放在assets文件夹中。

enter image description here 然后在代码中

 @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

   Typeface font1= Typeface.createFromAsset(context.getAssets(), "Durwent.ttf");
 Typeface font2= Typeface.createFromAsset(context.getAssets(), "Fenwick.ttf");
}

 // you can apply font position wise 

  if (position == 0) {
        //set font 1 
         textView.setTypeface(font1);
    } 
 if (position == 1){
       //set font 2 
         textView.setTypeface(font2);
    }

--------------or-----------------

   if (position % 2 == 0) {
        //set font 1 
         textView.setTypeface(font1);
    } else {
       //set font 2 
         textView.setTypeface(font2);
    }
 }

答案 2 :(得分:0)

尝试一下:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    MyHolder myHolder = (MyHolder)holder;
    Current current = currentList.get(position);
    myHolder.textDescription.setText(current.getDescription());
    myHolder.textTitle.setText(current.getTitle());
    myHolder.textSource.setText(current.getRelease_date());
    myHolder.textType.setText(current.getContent_type());


    Typeface font1 = Typeface.createFromAsset(context.getAssets(), "myFirstFont.ttf");
    Typeface font2 = Typeface.createFromAsset(context.getAssets(), "mySecondFont.ttf");
   if(position % 2 ==0 )
   {
       myHolder.textDescription.setTypeface(font1);

   }else
       myHolder.textDescription.setTypeface(font2);


}