其他分享
首页 > 其他分享> > android – 如何通过多项选择重新调整Spinner中项目的高度?

android – 如何通过多项选择重新调整Spinner中项目的高度?

作者:互联网

我只是根据这个stackoverflow answer(see @Destil answer)显示微调器有多种选择.这里我的问题是我无法通过多种选择调整Spinner中项目的高度.如何重新调整每个项目的高度?

解决方法:

据我所知,您将不得不使用自定义适配器,并覆盖getDropDown和getView方法.您将能够自定义自定义布局的每个项目.

嗯……单词很好,例如更好,尝试类似的东西:

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

}

和布局:

<?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>

(但它只是一个示例,如果要根据选择调整大小,可以在每个对象上添加一个布尔mSelected并根据此布尔值更改视图)

编辑:然后在MultiSpinner中:

删除这个:

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

并在微调器中添加新的自定义适配器:

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

标签:android,android-spinner,android-dialog
来源: https://codeday.me/bug/20190826/1731464.html