简体   繁体   English

自定义微调器适配器

[英]Custom Spinner Adapter

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;
        }

Try this out 试试看

Inside your Layout File: 在布局文件中:

<Spinner
     android:id="@+id/spinnerview"
     android:layout_width="180dp"
     android:layout_height="42dp"
     android:layout_marginLeft="105dp"
     android:layout_marginTop="45dp"
     android:background="@drawable/spinner_back"
     android:paddingLeft="5dp"
     android:spinnerMode="dropdown"
     android:visibility="visible" />

inside your string.xml : 在您的string.xml

 <string-array name="spinner_array_environtment">
        <item>Test</item>
        <item>Production</item>
 </string-array>

inside you Java File in onCreate() Method: 在Java文件中的onCreate()方法中:

spinner_environment = (Spinner) findViewById(R.id.spinnerview);
            adapter = ArrayAdapter.createFromResource(this,
                    R.array.spinner_array_environtment, R.layout.spinner);
            adapter.setDropDownViewResource(R.layout.spinner);
            spinner_environment.setAdapter(adapter);

Make new spinner.xml file to your layout folder : 将新的spinner.xml文件添加到布局文件夹:

inside spinner.xml file : spinner.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerTarget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:textColor="#4C4646" />

Thats it!!! 而已!!!

Pass itemList as parameter in super class constructor 将itemList作为参数传递给超类构造函数

public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {

    super(context, textViewResourceId, itemList);
    this.context=context;
    this.itemList=itemList;
}

The simplest, I think :) 我认为最简单的是:)

List<String> listOfItems = getListOfItems(); // returns ArrayList<String>

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listOfItems);

targetSpinner.setAdapter(spinnerAdapter);

Ok, to simply put a list of string in a spinner shouldn't force us to implement an Adapter. 好的,仅将一个字符串列表放入微调器中,不应强迫我们实现适配器。 That's a code blot and getting a bit pattern crazy, I think. 我认为这是代码污点,并且使模式有些疯狂。

Trick is the simple_spinner_item id - damn, I like the R.id mechanism but this isn't intuitive from the docs. 窍门是simple_spinner_item id-该死,我喜欢R.id机制,但这在文档中并不直观。

this worked for me (ussing android.R.layout. simple_spinner_dropdown_item ): 这个工作对我来说(尤斯android.R.layout simple_spinner_dropdown_item。):

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CheckedTextView checkedTextView = (CheckedTextView) super.getView(position, convertView, parent);
    checkedTextView.setText(itemList.get(position));
    return checkedTextView;
}

i think it's a better solution because you don't inflate multiple times. 我认为这是一个更好的解决方案,因为您不会多次充气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM