当我在RecyclerView中使用自定义字体时出现错误

时间:2015-07-20 09:16:40

标签: java android

它是一个非常基本的RecyclerView列表应用程序,带有两个TextView和一个ImageView,它使用ArrayList类提取数据。有用。但我想用其中一个TextView设置自定义字体。我在哪里可以输入“字体代码”?

主要活动

    public class MainActivity extends ActionBarActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);


        mRecyclerView.setHasFixedSize(true);


        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);





        ArrayList<AllList> list = new ArrayList<AllList>();


        list.add(new AllList("test","description",R.drawable.arrow));

        mAdapter = new MyAdapter(list);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

适配器类

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<AllList> list;
    Typeface ef;
    Context c;


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public View View;
        public ViewHolder(View v) {
            super(v);
            View = v;
        }
    }


    public MyAdapter(ArrayList<AllList> duas) {


        this.list = duas;
    }


    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row, parent, false);



        ViewHolder vh = new ViewHolder(v);

        return vh;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


       TextView title = (TextView) holder.View.findViewById(R.id.title);
       TextView desc = (TextView) holder.View.findViewById(R.id.desc);
        ImageView imageView = (ImageView) holder.View.findViewById(R.id.imageView);


//        Typeface cfont = Typeface.createFromAsset(getContext().getAssets(), "fonts.h.ttf");
//        this.ef = cfont;
//
////        title.setTypeface(ef);

        title.setText(list.get(position).getTitle());
        desc.setText(list.get(position).getDesc());

    }


    @Override
    public int getItemCount() {
        return list.size();
    }
}

1 个答案:

答案 0 :(得分:0)

使用此代码

public class CustomTextView extends TextView {

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    createFont();
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    createFont();
}

public CustomTextView(Context context) {
    super(context);
    createFont();
}

public void createFont() {
    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "roboto_light.ttf");
    setTypeface(font);
}
}

在XML中使用此类

 <CustomTextView
     android:id="@+id/title" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
 />

最后你必须使用这个textview刚刚初始化并使用它

CustomTextView title = (CustomTextView) findViewById(R.id.title);
title.setText("Enjoy!!");