37

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

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

}

然后我用

List<CharSequence> itemList = new ArrayList<CharSequence>(
            Arrays.asList(items));

    mySpinnerArrayAdapter = new   CustomAdapter(context,android.R.layout.simple_spinner_item,itemList); 
    spinner.setAdapter(mySpinnerArrayAdapter);

这样做之后,我的适配器是空的。谁能帮帮我吗 ?这些项目包含国家/地区列表。

亲切的问候,

4

5 回答 5

56

尝试

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;
        }
于 2012-09-26T10:35:50.540 回答
4

在超类构造函数中将 itemList 作为参数传递

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

    super(context, textViewResourceId, itemList);
    this.context=context;
    this.itemList=itemList;
}
于 2014-11-07T12:52:39.730 回答
2

试试这个

在您的布局文件中:

<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" />

在你的里面string.xml

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

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

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

内部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" />

而已!!!

于 2012-09-26T10:45:27.023 回答
2

最简单的,我认为:)

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

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

targetSpinner.setAdapter(spinnerAdapter);

好的,简单地把一个字符串列表放在一个微调器中不应该强迫我们实现一个适配器。我认为这是一个代码污点,并且让模式有点疯狂。

技巧是 simple_spinner_item id - 该死的,我喜欢 R.id 机制,但这在文档中并不直观。

于 2014-06-07T06:07:18.063 回答
0

这对我有用(使用 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;
}

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

于 2016-03-29T16:44:59.457 回答