其他分享
首页 > 其他分享> > android – 如何将Dropdown微调器锚定到父Linearlayout?

android – 如何将Dropdown微调器锚定到父Linearlayout?

作者:互联网

如何将Dropdown微调器锚定到一个相同的Linearlayout?

我在水平LinearLayout中有一个TextView和一个微调器.我想将LinearLayout设置为微调器下拉列表的锚点.就像你可以使用AutoCompleteTextViews下拉菜单一样.

        <LinearLayout
            android:id="@+id/parent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="bottom"
            android:weightSum="1" >

            <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.CustomSpinner
                android:id="@+id/customSpinner"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".2"
                android:drawSelectorOnTop="true"
                android:gravity="bottom"/>
        </LinearLayout>

解决方法:

我发现最简单的方法是扩展ImageButton并添加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>

布局:
        

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

标签:android,android-spinner
来源: https://codeday.me/bug/20191005/1856665.html