自定义微调适配器

编程入门 行业动态 更新时间:2024-10-25 04:26:26
本文介绍了自定义微调适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想自定义字体应用到我的微调。我发现的唯一方法是创建一个自定义的适配器。这是我的code

私有类CustomAdapter扩展ArrayAdapter {     私人上下文的背景下;     私人列表< CharSequence的> ITEMLIST;     公共CustomAdapter(上下文的背景下,INT textViewResourceId,名单,其中,为CharSequence> ITEMLIST){         超(背景下,textViewResourceId);         this.context =背景;         this.itemList = ITEMLIST;     }     公众的TextView getView(INT位置,查看convertView,ViewGroup中父){         TextView的V =(TextView中)超                 .getView(位置,convertView,父母);         字样myTypeFace = Typeface.createFromAsset(context.getAssets()                 字体/ gilsanslight.otf);         v.setTypeface(myTypeFace);         v.setText(itemList.get(位置));         返回伏;     }     公众的TextView getDropDownView(INT位置,查看convertView,             ViewGroup中父){         TextView的V =(TextView中)超                 .getView(位置,convertView,父母);         字样myTypeFace = Typeface.createFromAsset(context.getAssets()                 字体/ gilsanslight.otf);         v.setTypeface(myTypeFace);         v.setText(itemList.get(位置));         返回伏;     } }

然后使用

名单,其中,为CharSequence> ITEMLIST =新的ArrayList< CharSequence的>(             Arrays.asList(项目));     mySpinnerArrayAdapter =新CustomAdapter(背景下,android.R.layout.simple_spinner_item,ITEMLIST);     spinner.setAdapter(mySpinnerArrayAdapter);

这样做了以后,我的适配器是空的。谁能帮帮我吗?项目包含国家的列表。

亲切的问候,

解决方案 尝试

公开查看getView(INT位置,查看convertView,ViewGroup中父){     LayoutInflater充气= getLayoutInflater();             查看排= inflater.inflate(yourRowlayout,父母,                     假);        TextView中使=(TextView中)row.findViewById(R.id.Make);         字样myTypeFace = Typeface.createFromAsset(context.getAssets()                 字体/ gilsanslight.otf);         v.setTypeface(myTypeFace);         v.setText(itemList.get(位置));         返回行;     } 公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){         LayoutInflater充气= getLayoutInflater();                 查看排= inflater.inflate(yourRowlayout,父母,                         假);            TextView中使=(TextView中)row.findViewById(R.id.Make);             字样myTypeFace = Typeface.createFromAsset(context.getAssets()                     字体/ gilsanslight.otf);             v.setTypeface(myTypeFace);             v.setText(itemList.get(位置));             返回行;         }

I wanted to apply a custom font to my spinner. The only way i found out is that to create a custom adapter. Here is my code

private class CustomAdapter extends ArrayAdapter { private Context context; private List<CharSequence> itemList; public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) { super(context, textViewResourceId); this.context=context; this.itemList=itemList; } public TextView getView(int position, View convertView, ViewGroup parent) { TextView v = (TextView) super .getView(position, convertView, parent); Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/gilsanslight.otf"); v.setTypeface(myTypeFace); v.setText(itemList.get(position)); return v; } public TextView getDropDownView(int position, View convertView, ViewGroup parent) { TextView v = (TextView) super .getView(position, convertView, parent); Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/gilsanslight.otf"); v.setTypeface(myTypeFace); v.setText(itemList.get(position)); return v; } }

Then i use

List<CharSequence> itemList = new ArrayList<CharSequence>( Arrays.asList(items)); mySpinnerArrayAdapter = new CustomAdapter(context,android.R.layout.simple_spinner_item,itemList); spinner.setAdapter(mySpinnerArrayAdapter);

After doing this, my adapter is empty. Can anyone please help me ? The items contains a list of countries.

Kind Regards,

解决方案

Try

public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View row = inflater.inflate(yourRowlayout, parent, false); TextView make = (TextView) row.findViewById(R.id.Make); Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/gilsanslight.otf"); v.setTypeface(myTypeFace); v.setText(itemList.get(position)); return row; } public View getDropDownView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View row = inflater.inflate(yourRowlayout, parent, false); TextView make = (TextView) row.findViewById(R.id.Make); Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/gilsanslight.otf"); v.setTypeface(myTypeFace); v.setText(itemList.get(position)); return row; }

更多推荐

自定义微调适配器

本文发布于:2023-11-26 21:30:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   适配器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!