Android 自定义微调适配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12599703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:09:29  来源:igfitidea点击:

Custom Spinner Adapter

android

提问by AndroidDev

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,

亲切的问候,

回答by mukesh

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

回答by Bhavesh Patadiya

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:

onCreate()方法中的Java 文件中:

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.xmlfile to your layout folder :

spinner.xml在您的布局文件夹中创建新文件:

inside spinner.xmlfile :

内部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!!!

就是这样!!!

回答by Vipin Sharma

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

回答by Otpidus

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 机制,但这从文档中看并不直观。

回答by Ninja Coding

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.

我认为这是一个更好的解决方案,因为您不会多次充气。