自定义微调适配器 [英] Custom Spinner Adapter

查看:120
本文介绍了自定义微调适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义字体应用到我的微调。我发现的唯一方法是创建一个自定义的适配器。这是我的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;
        }

这篇关于自定义微调适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆