【问题标题】:Set custom typeface for arrayadapter为 arrayadapter 设置自定义字体
【发布时间】:2016-05-06 21:28:42
【问题描述】:

这是我的代码:

String[] filled_arr;
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.list_item, R.id.tv_wi, filled_arr);
listview.setAdapter(adapter);

这是字体:

final Typeface typeface = Typeface.createFromAsset(getAssets(), "Constantia.ttf");

如何为适配器设置?

【问题讨论】:

    标签: android android-arrayadapter typeface


    【解决方案1】:

    我已经通过覆盖数组适配器解决了这个问题。希望,它会帮助别人)

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, filled_arr) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView text = (TextView) view.findViewById(android.R.id.text1);
                text.setTypeface(typeface);
                return view;
            }
        };
    

    【讨论】:

    • TextView text = (TextView) view.findViewById(android.R.id.text1); text1 在运行时无法找到。作为 NullPointer 异常在文本上引发错误。有解决办法吗?
    【解决方案2】:
    public class CustomAdapter extends ArrayAdapter<DataModel> {
    
        private ArrayList<DataModel> dataSet;
        Context mContext;
    
    
        // View lookup cache
        private static class ViewHolder {
            TextView txtName;
            CheckBox checkBox;
        }
    
        public CustomAdapter(ArrayList<DataModel> data, Context context) {
            super(context, R.layout.fathers_meet_text_check_layout, data);
            this.dataSet = data;
            this.mContext = context;
    
        }
        @Override
        public int getCount() {
            return dataSet.size();
        }
    
        @Override
        public DataModel getItem(int position) {
            return dataSet.get(position);
        }
    
    
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    
            final ViewHolder viewHolder;
            final View result;
            View v = convertView;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fathers_meet_text_check_layout, parent, false);
                viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
                viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
    
    
                result=convertView;
                convertView.setTag(viewHolder);
    
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
                result=convertView;
            }
    
            DataModel item = getItem(position);
    
            //use typeface
            Typeface customfont=Typeface.createFromAsset(parent.getContext().getAssets(),"VANAVILAvvaiyar.otf");
    
            viewHolder.txtName.setText(item.name);
    
            //set typeface
            viewHolder.txtName.setTypeface(customfont);
                viewHolder.checkBox.setChecked(item.checked);
    
    
    
    
            return result;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2022-12-23
      • 1970-01-01
      相关资源
      最近更新 更多