Android下拉框更改字体样式

4
我将在我的下拉列表中更改文本字体为Arial Bold,我该如何做呢?以下是我的代码:-
ArrayAdapter<CharSequence> genderAdap = ArrayAdapter.createFromResource(getActivity(), R.array.gender,R.layout.spinner_item);


genderAdap.setDropDownViewResource(R.layout.spinner_item);

ddlGender.setAdapter(genderAdap);
5个回答

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

    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <Spinner  
     android:id="@+id/spinner1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content" />  
 </LinearLayout> 

我的_spinner_style.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Large Text"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </LinearLayout> 

TTF文件


我尝试了这个,但仍然不起作用。在代码后面,我添加了adapter.setDropDownViewResource(R.layout.spinner_item); ddlGender.setAdapter(adapter); - nick
你的意思是数据没有填充吗? - KOTIOS

3

以下是如何操作,将您的适配器设置如下:

mMsgAdap =new ArrayAdapter<CharSequence>(getApplicationContext(), R.layout.spinner_item_list, mCategories)
                {

                    public View getView(int position, View convertView, ViewGroup parent) 
                        {
                            View v = super.getView(position, convertView, parent);
                            ((TextView) v).setTypeface(StaticUtils.sTypeFace(getApplicationContext()));//Typeface for normal view

                            return v;
                        }
                    public View getDropDownView(int position, View convertView, ViewGroup parent) 
                        {
                            View v = super.getDropDownView(position, convertView, parent);
                            ((TextView) v).setTypeface(StaticUtils.sTypeFace(getApplicationContext()));//Typeface for dropdown view
                            ((TextView) v).setBackgroundColor(Color.parseColor("#BBfef3da"));
                            return v;
                        }
                };
        mMsgAdap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mMsgSpnr.setAdapter(mMsgAdap);
        mMsgSpnr.setSelection(0);

我创建了一个名为 StaticUtils 的类,并在其中静态地使用了字体方法。

public static Typeface sTypeFace(Context mCnxt) {
    Typeface mtypeface = Typeface.createFromAsset(mCnxt.getAssets(),
            "fonts/forte-mt-1361539051.ttf");
    return mtypeface;
}

@nick 你的数据存储在哪里? - user3513843
在R.array.country中,我将mCategories更改为R.array.country。 - nick
我建议您将数据存储在活动中的ArrayList<String>中,并放置该ArrayList<String>对象而不是mCategories。那肯定会起作用。但是,我需要检查如何通过资源传递值,因为我以前没有尝试过。 - user3513843
或者传递一个字符串数组,例如 String [] countryArray = {"美国", "英国", "印度", "中国"}; - user3513843

0

如果要更改下拉列表项的字体,您需要使用自定义 Spinner

尝试为 Spinner 中的所有 TextView 设置自定义 Typeface:

Typeface typeface;
font_name_Adapter= new ArrayAdapter<String>
                (this,android.R.layout.simple_spinner_item,ARRLIST_FONTS)
       {
            public View getView(int position, View convertView,ViewGroup parent) {
               View v = super.getView(position, convertView, parent);
               ((TextView) v).setTextSize(12);
               ((TextView) v).setTextColor(Color.WHITE);

                if(position<=ARRLIST_FONTS.size()){
                 typeface = 
                  Typeface.createFromAsset(Your_Current_Activity.this.getAssets(),
                               "fonts/yourfontname.ttf"); 
                     ((TextView) v).setTypeface(typeface);
                   }

                    return v;
     }

1
@nick 请查看以下链接,了解有关自定义Spinner适配器的信息:https://dev59.com/Qmkw5IYBdhLWcg3wVpBihttp://karanbalkar.com/2013/07/tutorial-39-create-custom-spinner-in-android/ - Sanket Shah

0

你可以通过自定义的SpinnerAdapter,在getView()和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;
        }

0
// try this custom adapter for spinner
class MyCustomAdapter extends ArrayAdapter<String> {

        Context context;
        ArrayList<String> list;
        private int defaultPosition;

        public int getDefaultPosition() {
            return defaultPosition;
        }

        public MyCustomAdapter(Context context, ArrayList<String> objects) {
            super(context, 0, objects);
            this.context = context;
            list = objects;
        }

        public void setDefaultPostion(int position) {
            this.defaultPosition = position;
        }

        @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 getCustom(position, convertView, parent);
        }

        public View getCustom(int position, View convertView, ViewGroup parent) {

            View row = LayoutInflater.from(context).inflate(
                    android.R.layout.simple_spinner_item, parent, false);
            TextView label = (TextView) row.findViewById(android.R.id.text1);
            Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontsname");
            label.setTypeface(tf);
            label.setText(list.get(position));

            return row;
        }

        public View getCustomView(int position, View convertView,
                ViewGroup parent) {

            View row = LayoutInflater.from(context).inflate(
                    android.R.layout.simple_spinner_item, parent, false);
            TextView label = (TextView) row.findViewById(android.R.id.text1);
            Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/yourfontname");
            label.setTypeface(tf);
            label.setText(list.get(position));

            return row;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接