不行,這是我的自定義文本類:文本樣式在自定義文本
public class CustomTXT extends TextView {
public CustomTXT(Context context) {
super(context);
Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
this.setTypeface(face);
}
public CustomTXT(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
this.setTypeface(face);
}
public CustomTXT(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
this.setTypeface(face);
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
但在XML佈局時使用的android:TEXTSTYLE =「黑體」自定義文本,文本樣式不行! 我嘗試以編程方式使用一套文本樣式是這樣的:
title = (TextView)view.findViewById(R.id.post_title);
title.setTypeface(title.getTypeface(), Typeface.BOLD);
工作正常,但當recyclerview應用程序崩潰的成爲終點,並給這個錯誤:
java.lang.NullPointerException at com.example.erfan.memaraneha.maghalat.DataAdapter$ViewHolder.<init>(DataAdapter.java:94)
從這一行:
title.setTypeface(title.getTypeface(), Typeface.BOLD);
這是我的適配器:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private Context context;
List<jsonContent> jcontent;
public DataAdapter(Context context,List<jsonContent> jcontent) {
this.context=context;
this.jcontent=jcontent;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view;
if(i == R.layout.card_row) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
}else {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.button_card, viewGroup, false);
}
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder,int i) {
if(i == jcontent.size()) {
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Button Clicked", Toast.LENGTH_LONG).show();
}
});
}
else {
viewHolder.title.setText(jcontent.get(i).title);
Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView);
}
}
@Override
public int getItemCount() {
return jcontent.size()+1;
}
@Override
public int getItemViewType(int position) {
return (position == jcontent.size()) ? R.layout.button_card : R.layout.card_row;
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private ImageView imageView;
private Button button;
public ViewHolder(final View view) {
super(view);
title = (TextView)view.findViewById(R.id.post_title);
title.setTypeface(title.getTypeface(), Typeface.BOLD);
imageView=(ImageView)view.findViewById(R.id.img);
button=(Button)view.findViewById(R.id.loadmore);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(view.getContext(),card_activity.class);
Bundle b = new Bundle();
String passingdata = title.getText().toString();
b.putString("Key", passingdata);
intent.putExtras(b);
view.getContext().startActivity(intent);
}
});
}
}
}
嘗試設置它*編程*。 –
@jaydroider嘗試這種方式,但我的文本視圖在適配器和當我設置字體爲我的txtview在適配器應用程序給我null異常錯誤 – erfan
發佈上述問題的代碼與異常日誌。 –