Android:如何在由ArrayAdapter.createFromResource填充的微调器中更改TypeFace?

时间:2013-12-09 15:29:46

标签: android spinner android-arrayadapter typeface

我喜欢将我的旋转器字体改为“等宽”。

现在我正以这种方式填充我的微调器:

resourceId = this.getResources().getIdentifier(country+"_umsatzsteuer_view", "array", this.getPackageName());
resourceId2 = this.getResources().getIdentifier(country+"_umsatzsteuer_werte", "array", this.getPackageName());


final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
          this, resourceId, android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
spinner_steuer = (Spinner) findViewById(R.id.spinner_steuer);
spinner_steuer.setAdapter(adapter);

我的问题是,如何将每个项目的字体从这个微调器设置为“等宽”?

更新代码:

Adaptercode:

public class YourItemAdapter extends ArrayAdapter<String> {
    public YourItemAdapter(Context _context, int _resource,
            List<String> _items) {

        super(_context, _resource, _items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        //You can use the new tf here.
        View v =super.getDropDownView(position, convertView, parent);
        ((TextView) v).setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
        return v;
        }
    }

Spinnerloading:

            final ArrayAdapter<CharSequence> adapter = YourItemAdapter.createFromResource(
                      this, resourceId, android.R.layout.simple_spinner_item );
                    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );

    spinner_steuer = (Spinner) findViewById(R.id.spinner_steuer);
    spinner_steuer.setAdapter(adapter);

提前致谢! MSeiz5

2 个答案:

答案 0 :(得分:1)

我自己解决了这个问题!我创建了一个自定义适配器和一个自定义微调器。

代码(JAVA) - (Mainsite.java)

String[] str_steuern;

        resourceId = this.getResources().getIdentifier(country+"_umsatzsteuer_view", "array", this.getPackageName());


        Resources steuernt = getResources();
        str_steuern = steuernt.getStringArray(resourceId);
        spinner_steuer = (Spinner) findViewById(R.id.spinner_steuer);
        spinner_steuer.setAdapter(new adapter_spinner_steuer(Mainsite.this, R.layout.spinner_steuer_row, str_steuern)); 

    public class adapter_spinner_steuer extends ArrayAdapter<String>{

        public adapter_spinner_steuer(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.spinner_steuer_row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.steuer);
            label.setText(str_steuern[position]);
            return row;
            }
        }

XML:(spinner_steuer_row.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>

    <TextView
        android:id="@+id/steuer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:typeface="monospace" />

</RelativeLayout>

答案 1 :(得分:0)

我建议你在另一个文件中实现你的适配器,然后你可以改变类型face,也可以从适配器的构造函数访问“getAssets()”函数,因为你有Context作为参数。 / p>

public class YourItemAdapter extends ArrayAdapter<String> {
int recurso;
Typeface tf;

public YourItemAdapter(Context _context, int _resource,
        List<String> _items) {

    super(_context, _resource, _items);
    recurso=_resource;
    tf=Typeface.createFromAsset(_context.getAssets(),"font/digital-7.ttf");
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //You can use the new tf here.
    TextView spinner_text=(TextView)findViewById(R.id.text1);
    spinner_text.setTypeface(tf);
    }
}