用于自定义字体的Android微调器

时间:2013-05-07 04:19:03

标签: android android-spinner

我遇到的问题是我想在以下编码中为android spinner添加自定义字体。我已经尝试过使用ArrayAdapter,但还不能成功。感谢。

private void loadSpinner() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");      
        list.add("9");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cmbStage.setAdapter(dataAdapter);
        cmbStage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Object item = parent.getItemIdAtPosition(pos + 1);
                currentID = Integer.parseInt(item.toString()); 
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });        
    }

1 个答案:

答案 0 :(得分:3)

您可以在SpinnerAdaptergetView()

中通过自己的自定义getDropDownView()应用字体
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;
        }