Android自定义微调器不填充

时间:2013-09-20 04:14:47

标签: android string fonts android-arrayadapter android-spinner

无法让我的微调器填充。好吧,我正在使用strings.xml中的字符串数组填充它,但现在我想为数组添加自定义字体。我第一次使用Custom ArrayAdapter,它在布局xml中覆盖了我的“android:entries =”和“android:prompt =”。

因此,我需要使用strings.xml中的数组填充此微调器

(在onCreate里面)

    spinner1 = (Spinner) findViewById(R.id.distancespinner);
    MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
    spinner1.setAdapter(ma);

然后:

 private class MyArrayAdapter extends ArrayAdapter {

    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");  

    public MyArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    }

    public TextView getView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    v.setTypeface(font);
    return v;
    }

    public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    v.setTypeface(font);
    return v;
    }

    }

我尝试在my_spinner_style.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:entries="@array/location_arrays"
android:prompt="@string/location_prompt"
android:singleLine="True" />

但是,这也不起作用。

3 个答案:

答案 0 :(得分:3)

也许这有点晚了。在我实现你的代码和一些额外的测试后,我想我终于得到了答案。微调器没有填充的KEY问题是:

您可以在xml文件中设置微调器的条目,但以编程方式将其替换为EMPTY适配器。

我尝试了以下方法来填充微调器,结果不同:

  1. 在没有任何适配器的情况下在xml中设置条目。

    <Spinner 
    android:id="@+id/simple_spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/simpleStringArray"
    android:prompt="@string/simplePrompt"/>
    

    它使用正确的内容填充微调器,而没有任何其他样式。

  2. 使用另一个设置为微调器的适配器设置xml中的条目(就像你在问题中所做的那样)

    in .xml

    <Spinner 
    android:id="@+id/simple_spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/simpleStringArray"
    android:prompt="@string/simplePrompt"/>
    
    <。>在.java中:

    Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
    MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
    

    它显示一个空的微调器。

  3. 以编程方式将数据设置为适配器

    在.xml中     

    <。>在.java中         的setContentView(R.layout.activity_spinner);

    Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
    MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
    spinnerAdapter2.addAll(items2);
    

    私有类MySpinnerAdapter2扩展了ArrayAdapter {

    public MySpinnerAdapter2(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView v = (TextView) super.getView(position, convertView, parent);
        v.setTextColor(Color.BLUE);
        return v;
    }
    
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/Igualb.ttf");
        TextView v = (TextView) super.getDropDownView(position, convertView, parent);
        v.setTextColor(Color.RED);
        v.setTypeface(myFont);
        return v;
    }
    
  4. 使用cumtom-set字体正确显示内容。

    如果您愿意,可以在this github repository中查看完整的项目。

    希望这有帮助。

答案 1 :(得分:1)

这是我重写的代码,它工作正常:

Spinner spinner = (Spinner) findViewById(R.id.distancespinner);
spinner.setPromptId(R.string.location_prompt);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
ma.addAll(Arrays.asList(getResources().getStringArray(
        R.array.location_arrays)));
spinner.setAdapter(ma);

和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:singleLine="True" />

Adapter是一样的。

答案 2 :(得分:0)

可能是以下行不正确

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");

m01.TTFm01.ttf

的错误更改
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.ttf");
相关问题