1

How can I programmatically change the Padding and TextSize of the items in the spinner's dropdown box?

I tried using XML but the padding and text size appears different on different screen sizes. I need to perform a couple of simple calculations based on the dimensions of the screen. I tried inflating the view, but I found no way to convert the the view back to XML or apply it to my adapter.

Here is my code for the spinner:

View view = getLayoutInflater().inflate(R.layout.list_items, null);
view.setPadding((int) (screenWidth * 0.1), (int) (screenHeight * 0.2), (int) (screenWidth * 0.1), (int) (screenHeight * 0.2));
if (portrait)
    spnVerbs.setAdapter(new MySpinnerAdapter(R.layout.list_items, CHOICES));
else
    spnVerbs.setAdapter(new MySpinnerAdapter(R.layout.list_items1, CHOICES));

And MySpinnerAdapter.class

class MySpinnerAdapter extends ArrayAdapter<String>
{
    MySpinnerAdapter(int dropDownResource, String[] choices)
    {
        super(Start.this, dropDownResource, choices);
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
            row = super.getView(position, convertView, parent);
        if (parent.getWidth() > 0)
        {
            TextView label = (TextView) row.findViewById(android.R.id.text1);
            label.setIncludeFontPadding(false);
            label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), parent.getWidth() - paddingLeft * 2 - paddingLeftExtra * 2, (int) fieldHeight - paddingTop * 2 - paddingTopExtra * 2));
            label.setPadding(paddingLeft + paddingLeftExtra, 0, 0, 0);
        }
        return(row);
    }
}
4

2 回答 2

3

I added the following function to MySpinnerAdapter and it works perfectly now:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
    View view = super.getDropDownView(position, convertView, parent);
    TextView textView = (TextView) view.findViewById(android.R.id.text1);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
    textView.setPadding(paddingLeft + paddingLeftExtra, paddingTop + paddingTopExtra, paddingLeft + paddingLeftExtra, paddingTop + paddingTopExtra);
    return view;
}

Also, I applied simple_spinner_item, instead of simple_list_item_1 or my own XML file. simple_spinner_item works well because it doesn't apply any minimum values to the padding.

于 2013-07-09T18:47:54.767 回答
0

First of all set a adapter for your spinner like this:

ArrayAdapter<String> ageAdapter; = new ArrayAdapter<String>(this, R.layout.spinner_item, age);
yourSpinner.setAdapter(yourAdapter);

Now create a xml file in your layout folder named spinner_item like this:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textSize="17sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@drawable/tool1" />

This will might help you..

于 2013-07-09T05:21:38.783 回答