Spinner就像edittext一样

Yam*_*ila 2 android android-spinner

我有一个TextInputLayout + TextInputEditText和微调器的表单.我想要微调器高度换行文本,但在下拉列表中有大项目.问题是微调器的高度取决于下拉项高度(simple_spinner_dropdown_item).我设置style ="@ style/Base.Widget.AppCompat.Spinner.Underlined"以在spinner下面添加一行.

有解决方案吗

RoS*_*han 10

Spinner就像edittext一样

如果你想要像edittext这样的微调器,它就像 AutoCompleteTextView.你可以自定义你AppCompatAutoCompleteTextView喜欢的:

public class AutoCompleteDropDown extends AppCompatAutoCompleteTextView {
    //    implements AdapterView.OnItemClickListener
    private static final int MAX_CLICK_DURATION = 200;
    private long startClickTime;
    private boolean isPopup;
    private int mPosition = ListView.INVALID_POSITION;

    public AutoCompleteDropDown(Context context) {
        super(context);
//        setOnItemClickListener(this);
    }

    public AutoCompleteDropDown(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
//        setOnItemClickListener(this);
    }

    public AutoCompleteDropDown(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
//        setOnItemClickListener(this);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
                                  Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            performFiltering("", 0);
            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindowToken(), 0);
            setKeyListener(null);
            dismissDropDown();
        } else {
            isPopup = false;
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP: {
                if (isPopup) {
                    dismissDropDown();
                } else {
                    requestFocus();
                    showDropDown();
                }
                break;
            }
        }

        return super.onTouchEvent(event);
    }

    @Override
    public void showDropDown() {
        super.showDropDown();
        isPopup = true;
    }

    @Override
    public void dismissDropDown() {
        super.dismissDropDown();
        isPopup = false;
    }



    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        Drawable dropdownIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_expand_more_black_18dp);
        if (dropdownIcon != null) {
            right = dropdownIcon;
            right.mutate().setAlpha(66);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            super.setCompoundDrawablesRelativeWithIntrinsicBounds(left, top, right, bottom);
        } else {
            super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
        }

    }



    public int getPosition() {
        return mPosition;
    }
}
Run Code Online (Sandbox Code Playgroud)

ic_expand_more_black_18dp.png是一个像这样的图像:

布局:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColorHint="@color/gray_text_hint"
        app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout.Dark">

        <yourpackage.AutoCompleteDropDown
            android:id="@+id/edtBloodType"
            style="@style/edt_dark"
            android:hint="Blood Type"
            android:inputType="textNoSuggestions" />
    </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

结果(您可以为AutoCompleteTextView设置适配器)

在此输入图像描述