我正在尝试在我的应用程序中使用超棒的字体。但是我不知道如何在不传递上下文作为参数的情况下使用它。这是我的xml文件:
<Button
android:layout_below="@id/author"
android:id="@+id/compare_btn"
android:layout_toRightOf="@id/cart_btn"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="@string/fa_random" />
这是我的Holder类,它当然不会扩展活动范围或片段。
public class ListRowViewHolder extends RecyclerView.ViewHolder {
protected NetworkImageView productImageURL;
protected TextView review;
protected TextView productName;
protected RelativeLayout recLayout;
protected TextView categoryId;
protected TextView price;
protected Button wishListBtn, cartBtn, compareBtn;
public ListRowViewHolder(View view) {
super(view);
this.productImageURL = (NetworkImageView) view.findViewById(R.id.thumbnail);
this.productName = (TextView) view.findViewById(R.id.title);
this.review = (TextView) view.findViewById(R.id.url);
this.recLayout = (RelativeLayout) view.findViewById(R.id.recLayout);
this.categoryId = (TextView) view.findViewById(R.id.subreddit);
this.price = (TextView) view.findViewById(R.id.author);
this.wishListBtn=(Button) view.findViewById(R.id.wishlist_btn);
this.cartBtn=(Button) view.findViewById(R.id.cart_btn);//set Font Awesome here
this.compareBtn=(Button) view.findViewById(R.id.customer_button);//
view.setClickable(false);
}
}
这是我的回收站适配器中的一种方法,该方法根据json数据设置textview的文本
@Override
public void onBindViewHolder(final ListRowViewHolder listRowViewHolder, int position) {
ListItems listItems = listItemsList.get(position);
listRowViewHolder.itemView.setSelected(focusedItem == position);
String local ="http://carrottech.com/lcart/media/catalog/product/"+listItems.getProductImageURL().trim();
System.out.print(local+"sdaaaaaaaaaaaaaaaaaaaaaa");
listRowViewHolder.getLayoutPosition();
mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
listRowViewHolder.productImageURL.setImageUrl(local, mImageLoader);
listRowViewHolder.productImageURL.setDefaultImageResId(R.drawable.reddit_placeholder);
listRowViewHolder.productName.setText(Html.fromHtml(listItems.getProductName()));
listRowViewHolder.categoryId.setText(Html.fromHtml(String.valueOf(listItems.getCategoryId())));
listRowViewHolder.price.setText(Html.fromHtml(String.valueOf(listItems.getProductPrice())));
listRowViewHolder.review.setText(Html.fromHtml("Write Review"));
listRowViewHolder.wishListBtn.setText(Html.fromHtml("&#f001;"));
}
我一直使用这样的字体类来设置Font很棒:
public class FontManager {
public static final String ROOT = "fonts/",
FONTAWESOME = "fonts/fontawesome-webfont.ttf";
public static Typeface getTypeface(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
}
}
在我过去的活动或片段中
Typeface iconCalendar = FontManager.getTypeface(getContext(), FontManager.FONTAWESOME);
jcustomerDobIcon.setTypeface(iconCalendar);
但是如果没有上下文传递给FontManager类,该怎么办?
请帮忙!
最佳答案
如果没有上下文传递给FontManager类,该怎么办
使用Context
方法从View的ListRowViewHolder
类中获取View.getContext()
。
就像在onBindViewHolder
方法中一样,将Context传递给FontManager
类为:
Typeface iconCalendar = FontManager.getTypeface(
listRowViewHolder.wishListBtn.getContext(),
FontManager.FONTAWESOME);
listRowViewHolder.wishListBtn.setTypeface(iconCalendar);
listRowViewHolder.wishListBtn.setText(Html.fromHtml("&#f001;"));