7

我正在用古吉拉特语开发一个 Android 应用程序,需要在 Spinner 小部件上设置古吉拉特语文本。所以,我从这里学习了如何在 Spinner 上设置自定义字体。

但问题是我得到的是盒子而不是实际的文本。我的微调器文本来自字符串资源。附上我的代码。

public class SpinnerExActivity extends Activity {
Spinner mySpinner; 
Typeface myFont;

String[] items; 
MyArrayAdapter ma;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex);
    mySpinner = (Spinner) findViewById(R.id.spinner1);
     items=getResources().getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
     ma=new MyArrayAdapter(this, R.layout.my_spinner_style); 
     ma.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


}
private class MyArrayAdapter extends ArrayAdapter {

    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(myFont);
    v.setText(items[position]);
    return v;
    }

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

    }

当我在模拟器上尝试这个时,我得到了盒子。但是当我在平板电脑上尝试时,我什么也没得到。

任何帮助都是不言而喻的。

编辑 这个作品:: 感谢这个。

public class SpinnerExActivity extends Activity {
Spinner mySpinner; 
Typeface myFont;

String[] items; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex);
    mySpinner = (Spinner) findViewById(R.id.spinner1);
     items=getResources().getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             R.layout.my_spinner_style, items) {

  public View getView(int position, View convertView, ViewGroup parent) {
          View v = super.getView(position, convertView, parent);

          Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
          ((TextView) v).setTypeface(externalFont);
          ((TextView) v).setText(items[position]);
          ((TextView) v).setTextColor(Color.BLACK);
          return v;
  }


  public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
           View v =super.getDropDownView(position, convertView, parent);

          Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
          ((TextView) v).setTypeface(externalFont);
          ((TextView) v).setText(items[position]);

          return v;
   }
   };


     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                                 
     mySpinner.setAdapter(adapter);

}
4

1 回答 1

2

你必须看看这个:

在此处输入图像描述

检查以下代码:

package com.example.main.util;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.testapp.R;

public class testActivity extends Activity {

    private static final String[] COUNTRIES = new String[] { "Belgium",
            "France", "Italy", "Germany", "Spain" };
    private Spinner mySpinner;
    private Typeface myFont;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newlay);

        mySpinner = (Spinner) findViewById(R.id.spinner1);
        myFont = Typeface.createFromAsset(getAssets(), "gujarti.ttf");
        MyArrayAdapter ma = new MyArrayAdapter(this);
        mySpinner.setAdapter(ma);
    }

    private class MyArrayAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public MyArrayAdapter(testActivity con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return COUNTRIES.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.my_spinner_style, null);
                holder = new ListContent();

                holder.name = (TextView) v.findViewById(R.id.textView1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.name.setTypeface(myFont);
            holder.name.setText("" + COUNTRIES[position]);

            return v;
        }

    }

    static class ListContent {

        TextView name;

    }
}

好运!

于 2013-04-05T05:39:38.187 回答