Android 自定义微调器/下拉菜单
在应用程序Astrid Tasks中,有一个按钮。当你按下按钮时,会出现一个下拉菜单 它基本上是一个微调器,但采用下拉列表形式Android 自定义微调器/下拉菜单,android,drop-down-menu,android-spinner,Android,Drop Down Menu,Android Spinner,在应用程序Astrid Tasks中,有一个按钮。当你按下按钮时,会出现一个下拉菜单 它基本上是一个微调器,但采用下拉列表形式 有人知道如何做类似的事情吗?这是我看不到的小部件吗?作为这篇文章的原始作者(我是Astrid的主要Android开发人员之一),我很乐意分享Astrid是如何做到这一点的。我将在这里发布基本信息,但您可以在我们的github repo()上找到更多详细信息。基本思想是按照hanry的建议扩展GreenDroid的QuickActionWidget。子类类似于: pu
有人知道如何做类似的事情吗?这是我看不到的小部件吗?作为这篇文章的原始作者(我是Astrid的主要Android开发人员之一),我很乐意分享Astrid是如何做到这一点的。我将在这里发布基本信息,但您可以在我们的github repo()上找到更多详细信息。基本思想是按照hanry的建议扩展GreenDroid的QuickActionWidget。子类类似于:
public class MenuPopover extends QuickActionWidget {
protected DisplayMetrics metrics;
protected LinearLayout content;
public MenuPopover(Context context) {
super(context);
setContentView(R.layout.my_layout);
content = (LinearLayout) getContentView().findViewById(R.id.content);
metrics = context.getResources().getDisplayMetrics();
setFocusable(true);
setTouchable(true);
}
@Override
protected void populateQuickActions(List<QuickAction> quickActions) {
// Do nothing
}
@Override
protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
contentView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(), MeasureSpec.EXACTLY),
ViewGroup.LayoutParams.WRAP_CONTENT);
int rootHeight = contentView.getMeasuredHeight();
int offsetY = getArrowOffsetY();
int dyTop = anchorRect.top;
int dyBottom = getScreenHeight() - anchorRect.bottom;
boolean onTop = (dyTop > dyBottom);
int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY : anchorRect.bottom - offsetY;
setWidgetSpecs(popupY, onTop);
}
}
创建弹出窗口的实例后,可以通过调用
menuPopover.show(anchorView);
这是一个稍微简化的版本——实际上,我们将一些附加信息、侦听器等附加到这些视图中,使它们在单击时实际执行操作。如果需要,可以在以下位置查看完整代码:类是com.todoroo.astrid.ui.MainMenuPover
谢谢你使用阿斯特里德 哇,太棒了!你真棒,谢谢!(附言,我爱阿斯特里德!)谢谢你让它开源!
public void addViewToContent(View v, OnClickListener listener) {
content.addView(v);
if (listener != null) {
v.setOnClickListener(listener);
}
}
menuPopover.show(anchorView);