如何将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 – 如何将Dropdown微调器锚定到父Linearlayout?全部内容,希望文章能够帮你解决android – 如何将Dropdown微调器锚定到父Linearlayout?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)