开发者

Set the TextSize to a text in spinner in android programatically

Hi I want to set the 开发者_如何学运维style and size for a text in spinner programatically(Dynamically).I don't use any resources in my app for this.So give me some suggestions for this


I dont think you can create this dynamically without overriding the behavior of the default layout resources . To create it using resources:

Create a layout file containing a TextView and define the size,colors and other style for this. And create an ArrayAdapter object and provide that layout file in your adapter alongwith the ID of the TextView.

Your layout file will be like this: spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
<TextView
 android:id="@+id/textview"
  android:layout_height="30dip"
 android:layout_width="wrap_content"
 android:textSize="20dip"
 android:textColor="#ccddaa"
/>
</LinearLayout>

Now you can use this in your code like this:

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);      
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,R.id.textview,Your_Array_of_items);      
mySpinner.setAdapter(adapter); 

Also you can create a custom ArrayAdapter and overriding the methods

getView() or getDropDownView()

inside this methods you can set custom color, size and fonts for your TextView

Update:

I have changed the text size and color of the spinner items dynamically by overrding the default resources of android. The snippet I have used is as given below:

public class CustomSpinner extends Activity {
String[] numbers = { "One", "Two", "Three", "Four", "Five" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this,
            android.R.layout.simple_spinner_item, numbers);
    spinner.setAdapter(adapter);
}

private class SpinnerAdapter extends ArrayAdapter<String> {
    Context context;
    String[] items = new String[] {};

    public SpinnerAdapter(final Context context,
            final int textViewResourceId, final String[] objects) {
        super(context, textViewResourceId, objects);
        this.items = objects;
        this.context = context;
    }

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

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(
                    android.R.layout.simple_spinner_item, parent, false);
        }

        TextView tv = (TextView) convertView
                .findViewById(android.R.id.text1);
        tv.setText(items[position]);
        tv.setTextColor(Color.BLUE);
        tv.setTextSize(30);
        return convertView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(
                    android.R.layout.simple_spinner_item, parent, false);
        }

        // android.R.id.text1 is default text view in resource of the android.
        // android.R.layout.simple_spinner_item is default layout in resources of android.

        TextView tv = (TextView) convertView
                .findViewById(android.R.id.text1);
        tv.setText(items[position]);
        tv.setTextColor(Color.BLUE);
        tv.setTextSize(30);
        return convertView;
    }
}


ArrayAdapter<String> ArrayAdapter_Price = new ArrayAdapter<String>(
      getActivity(), R.layout.item_list_drop_down, dollers) {

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

            View v = super.getView(position, convertView, parent);
            TextView tv = ((TextView) v);
            tv.setTextColor(getResources().getColor(R.color.login_text_color));
            tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.END);
            tv.setTextSize(18);
            return v;
        }
    };


I have made 2 modifications,

1-added another constructor so that one can use a resource 2-added 2 public function (setTextSize, and getTextSize) (3-Also disabled the color by putting them as comments)

public class CustomSpinner extends Activity {
String[] numbers = { "One", "Two", "Three", "Four", "Five" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this,
            android.R.layout.simple_spinner_item, numbers);

//(you can also pass in a string array resource like this:)
/*SpinnerAdapter adapter = new SpinnerAdapter(this,  R.layout.simple_spinner_item, 
   getResources().getStringArray(R.array.my_string_array_resource));*/
    spinner.setAdapter(adapter);
}


public class SpinnerAdapter extends ArrayAdapter<String> {
     Context context;
     String[] items = new String[] {};
     private int textSize=40; //initial default textsize  (might be a bit big)

        public SpinnerAdapter(final Context context, final int textViewResourceId, final String[] objects) {
            super(context, textViewResourceId, objects);
            this.items = objects;
            this.context = context;

        }
        public SpinnerAdapter(final Context context, final int resource, final int textViewResourceId ){
            super(context, resource, textViewResourceId);
            this.items = context.getResources().getStringArray(resource);

            Toast.makeText(context, String.valueOf(this.getSpinnerTextSize()), Toast.LENGTH_LONG).show();
        }


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

            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                convertView = inflater.inflate(
                        android.R.layout.simple_spinner_item, parent, false);
            }

            TextView tv = (TextView) convertView
                    .findViewById(android.R.id.text1);
            tv.setText(items[position]);
            //tv.setTextColor(Color.BLUE);
            tv.setTextSize(textSize);
            return convertView;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                convertView = inflater.inflate(
                        android.R.layout.simple_spinner_item, parent, false);
            }

            // android.R.id.text1 is default text view in resource of the android.
            // android.R.layout.simple_spinner_item is default layout in resources of android.

            TextView tv = (TextView) convertView
                    .findViewById(android.R.id.text1);
            tv.setText(items[position]);
            //tv.setTextColor(Color.BLUE);
            tv.setTextSize(textSize);
            return convertView;
        }

            //set the textsize
        public void setSpinnerTextSize(int size){

            textSize= size;
        }

            //return the textsize
        public int getSpinnerTextSize(){
            return textSize;
        }

}

}


same as custom listview we can set the values to the spinner .in the customview class we can set the properties to the Textview

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜