0

不行,這是我的自定義文本類:文本樣式在自定義文本

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); 
       } 
      }); 
     } 
    } 
} 
+0

嘗試設置它*編程*。 –

+0

@jaydroider嘗試這種方式,但我的文本視圖在適配器和當我設置字體爲我的txtview在適配器應用程序給我null異常錯誤 – erfan

+0

發佈上述問題的代碼與異常日誌。 –

回答

0

經過一番嘗試,我使用這條線

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

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

在我onBindViewHolder和解決問題

0

用這個,這是工作:

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

0

這條線之下設置字樣。將其從刪除它ViewHolder。您的字體 文件應該在資產/字體文件夾中。

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

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

viewHolder.title.setTypeface(face); 
+0

無法識別getAssets() – erfan

+0

檢查編輯答案。 –

+0

@erfan你完成了嗎? –

0

通行證文本樣式在您的自定義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代碼中明確設置文本樣式。