文本样式在自定义文本
问题描述:
不行,这是我的自定义文本类:文本样式在自定义文本
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);
}
});
}
}
}
答
经过一番尝试,我使用这条线
viewHolder.title.setTypeface(viewHolder.title.getTypeface(), Typeface.BOLD);
下
viewHolder.title.setText(jcontent.get(i).title);
在我onBindViewHolder和解决问题
答
用这个,这是工作:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
setTypeface(tf);
}
}
}
+0
不工作,我的代码似乎没有那么多不同! – erfan
答
这条线之下设置字样。将其从删除它ViewHolder。您的字体 文件应该在资产/字体文件夹中。
viewHolder.title.setText(jcontent.get(i).title);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/gandom-bold.ttf");
viewHolder.title.setTypeface(face);
答
通行证文本样式在您的自定义TextView中设置字体时。我改变了你的自定义Textview类。这将有希望地工作。
public class CustomTXT extends TextView {
public CustomTXT(Context context) {
super(context);
Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
this.setTypeface(face, Typeface.BOLD);
}
public CustomTXT(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
this.setTypeface(face, Typeface.BOLD);
}
public CustomTXT(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
this.setTypeface(face, Typeface.BOLD);
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
我setTypeFace方法添加“Typeface.BOLD”使您不再需要在XML或Java代码中明确设置文本样式。
尝试设置它*编程*。 –
@jaydroider尝试这种方式,但我的文本视图在适配器和当我设置字体为我的txtview在适配器应用程序给我null异常错误 – erfan
发布上述问题的代码与异常日志。 –