How to re size the Height of the Items in Spinner with multiple choice?

馋奶兔 提交于 2019-12-30 06:58:19

问题


I just showing the spinner with multiple choice based on this stackoverflow answer(see @Destil answer). Here my problem is I can't re size the Height of the items in Spinner with multiple choice. How to re size the Height of each Items?


回答1:


As i know, you will have to use a custom adapter, and override the getDropDown and the getView methods. You will be able to customize each item customizing the layout.

Well... words are good, example better, try something like that :

public class CustomStringArrayAdapter extends ArrayAdapter<String>
{
private Activity    myActivity;

public CustomStringArrayAdapter(Context context, int layoutResourceId, int textViewResourceId, List<String> objects, Activity act)
{
    super(context, layoutResourceId, textViewResourceId, objects);
    this.myActivity = act;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = myActivity.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_textview);
    label.setText(getItem(position));
    LinearLayout layout = (LinearLayout) row.findViewById(R.id.spinner_linear_layout);

    return row;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = myActivity.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_layout, parent, false);
    TextView label = (TextView) row.findViewById(R.id.spinner_textview);
    label.setText(getItem(position));

    return row;
    }

}

And the layout :

<?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="50dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="@drawable/transparent"
    android:id="@+id/spinner_linear_layout">
    <TextView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="21sp"
        android:singleLine="true"
        android:id="@+id/spinner_textview"
        android:textColor="@color/black" />
</LinearLayout>

(But it's just an sample, if you want to resize depending on the selection, you can add a boolean mSelected on each object and change the view depending on this boolean)

Edit : Then in the MultiSpinner :

Remove this :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_spinner_item,
            new String[] { spinnerText });
    setAdapter(adapter);

and add the new custom adapter in the spinner :

MultiSpinner mMultiSpinner = (MultiSpinner) findViewById(R.id.multispinner);
mMultiSpinner.setAdapter(mCustomArrayAdapter);


来源:https://stackoverflow.com/questions/8878260/how-to-re-size-the-height-of-the-items-in-spinner-with-multiple-choice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!