开发者

How to change the text size and orientation of a spinner

开发者 https://www.devze.com 2023-03-22 18:07 出处:网络
I need to change the text size and orientation of the selected item shown on a spinner, but also I need to continue using the default layout开发者_JAVA技巧 for the spinner: android.R.layout.simple_spi

I need to change the text size and orientation of the selected item shown on a spinner, but also I need to continue using the default layout开发者_JAVA技巧 for the spinner: android.R.layout.simple_spinner_item

Why? Because if I use personalized layout, I can't load dynamically generated arrays of strings on the spinner. It is a fail of Android :S

can someone help me please?


Ok, so it's a little tricky, but not too hard.

  1. Extend SimpleAdapter, creating a custom adapter.
  2. Make sure that you implement SpinnerAdapter in this new adapter. (if you are using Eclipse, let it help you by creating unimplemented methods)
  3. Implementing SpinnerAdapter will force you to add a dropdown view resource xml object. Add it statically, or define a setter.
  4. Inside public View getView(int position, View convertView, ViewGroup parent) (which is where you return a custom view to the Spinner), call parent.getSelectedItem() to see if it is the same as the item at position in your list.
  5. If it is, set your font bigger. If not, set it normal (don't forget to do this, as the spinner will recycle views, leaving your bigger font in place).

To use the adapter, call something like this from your Activity:

MyCustomSpinnerAdapter adapter = new MyCustomSpinnerAdapter(this, myListOfItems);
adapter.setDropDownViewResource(R.layout.my_dropdown_view_resource);
mySpinner.setAdapter(adapter);

To see how google does it, take a look in google codesearch here and search for "Android ArrayAdapter.java" - Click on the core/java/android/widget link and it'll take you to the package that has all the various adapters, interfaces and widgets that you need to figure the rest out.

Good luck.

EDIT: Here is an untested first crack at it as an example:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.beget.consumer.R;

public class MyCustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {

    private String[] list;
    private Context context;

    public MyCustomSpinnerAdapter(Context context, String[] list) {
        super();
        this.list = list;
        this.context = context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        return list.length;
    }

    @Override
    public Object getItem(int position) {
        //you need to add index safety here - make sure that position is a valid index
        return list[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = context.inflate(R.layout.my_view_item, null);
            holder = new ViewHolder();
            holder.myView = (TextView) convertView.findViewById(R.id.beget_box_description);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        if (list[position] == ((Spinner) parent).getSelectedItem()) {
            holder.myView.setTextSize(25);
        } else {
            holder.myView.setTextSize(15);
        }

        holder.myView.setText(list[position]);

        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        //change this if you want a different view for your dropdown list. It's the same as above, except you could 
        //inflate a different view if you wanted.
        return getView(position, convertView, parent);
    }

    static class ViewHolder {
        TextView myView;
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消