当前位置: 动力学知识库 > 问答 > 编程问答 >

android - text style not work in custom text

问题描述:

this is my custom text class:

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);

}

}

but in xml layout when use android:textStyle="bold" for custom text , text style not work!

i try to use set text style programmatically like this :

title = (TextView)view.findViewById(R.id.post_title);

title.setTypeface(title.getTypeface(), Typeface.BOLD);

work fine but when become end of recyclerview app crash and give this error :

java.lang.NullPointerException at com.example.erfan.memaraneha.maghalat.DataAdapter$ViewHolder.<init>(DataAdapter.java:94)

from this line :

 title.setTypeface(title.getTypeface(), Typeface.BOLD);

and this is my adapter :

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);

}

});

}

}

}

网友答案:

Use this, this is working :

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);
        }
    }
}
网友答案:

Set Type Face below this line. Remove it from ViewHolder. Your font file should be inside asset/fonts folder.

viewHolder.title.setText(jcontent.get(i).title);

Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/gandom-bold.ttf"); 

viewHolder.title.setTypeface(face);
网友答案:

after some try i use this line

viewHolder.title.setTypeface(viewHolder.title.getTypeface(), Typeface.BOLD);

under

viewHolder.title.setText(jcontent.get(i).title);

in my onBindViewHolder and problem solve

分享给朋友:
您可能感兴趣的文章:
随机阅读: