How do you anchor a Dropdown spinner to a parent Linearlayout?

后端 未结 1 2003
北荒
北荒 2021-01-01 07:57

How do you anchor a Dropdown spinner to a perent Linearlayout?

I have a TextView and a Spinner in a horizontal LinearLayout. I want to set the LinearLayout to be the

相关标签:
1条回答
  • 2021-01-01 08:45

    I found out that the easiest way was to extend a ImageButton and add a ListPopupWindow.

    public class MenuDropDown extends ImageButton {
    
        private ListPopupWindow mListDropDownWindow;
        private int mDropDownAnchorId;
        private ListAdapter mAdapter;
        private DropDownOnClickListener mDropDownOnClickListener;
        private OnItemClickListener mOnItemClickListener;
    
        public MenuDropDown(Context context, AttributeSet attrs) {
            this(context, attrs,R.attr.menuDropDown);
        }
    
        public MenuDropDown(Context context) {
            this(context, null);
        }
    
        public MenuDropDown(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mListDropDownWindow = new ListPopupWindow(context);
    
            mListDropDownWindow
                    .setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
            TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.MenuDropDown, defStyle, 0);
    
            mDropDownAnchorId = a.getResourceId(
                    R.styleable.MenuDropDown_dropDownAnchor,
                    View.NO_ID);
            mListDropDownWindow
                    .setOnItemClickListener(new DropDownItemClickListener());
            mListDropDownWindow.setModal(true);
            a.recycle();
            setFocusable(true);
    
    
            mDropDownOnClickListener = new DropDownOnClickListener();
            super.setOnClickListener(mDropDownOnClickListener);
        }
    
    
        private class DropDownItemClickListener implements
                AdapterView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
    
                dissmissDropDown();
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(parent, v, position, id);
                }
            }
        }
    
        private class DropDownOnClickListener implements OnClickListener {
    
            @Override
            public void onClick(View v) {
                showDropDown();
            }
        }
    
        private void dissmissDropDown() {
            mListDropDownWindow.dismiss();
    
        }
    
        public <T extends ListAdapter> void setAdapter(T adapter) {
            mAdapter = adapter;
            mListDropDownWindow.setAdapter(mAdapter);
        }
    
        public boolean isPopupShowing() {
            return mListDropDownWindow.isShowing();
        }
    
        public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
            mOnItemClickListener = listener;
        }
    
        private void showDropDown() {
            if (mListDropDownWindow.getAnchorView() == null) {
                if (mDropDownAnchorId != View.NO_ID) {
                    mListDropDownWindow.setAnchorView(getRootView().findViewById(
                            mDropDownAnchorId));
                } else {
                    mListDropDownWindow.setAnchorView(this);
                }
            }
            mListDropDownWindow.show();
            if (VERSION.SDK_INT >= 9) {
                mListDropDownWindow.getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
            }
    
        }
    
        public ListAdapter getAdapter() {
            return mAdapter;
        }
    }
    

    attrs.xml:

    <attr name="menuDropDown" format="reference" />
    
    <declare-styleable name="MenuDropDown" perent="ImageButton">
        <attr name="dropDownAnchor" format="reference" />
    </declare-styleable>
    

    Layout:

            <AutoCompleteTextView
                android:id="@+id/autoText"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".8"
                android:gravity="bottom"
                android:dropDownAnchor="@id/parent"/>
            <mycode.MenuDropDown
                android:id="@+id/customSpinner"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".2"
                android:drawSelectorOnTop="true"
                mycode:dropDownAnchor="@id/parent"
                android:gravity="bottom"/>
        </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题