如何将Dropdown微调器锚定到父级Linearlayout? [英] How do you anchor a Dropdown spinner to a parent Linearlayout?

查看:76
本文介绍了如何将Dropdown微调器锚定到父级Linearlayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Dropdown微调器锚定到永久的Linearlayout?

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

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

I have a TextView and a Spinner in a horizontal LinearLayout. I want to set the LinearLayout to be the anchor of the spinner drop down. Just like you can do with a AutoCompleteTextViews drop down.

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

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:

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>

这篇关于如何将Dropdown微调器锚定到父级Linearlayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆