Col*_*ole 19 android android-spinner drop-down-menu
在应用程序Astrid Tasks中,有一个按钮.当您按下按钮时,会出现一个下拉菜单.
它基本上是一个微调器,但是以下拉列表的形式出现.
有谁知道怎么做类似的事情?这是一个我看不到的小部件吗?
小智 55
作为这个的原作者(我是Astrid的主要Android开发人员之一),我很乐意分享Astrid是如何做到的.我将在这里发布基础知识,但您可以在我们的github repo(https://github.com/todoroo/astrid)上找到更多详细信息.根据汉利的建议,基本的想法是扩展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);
}
}
Run Code Online (Sandbox Code Playgroud)
布局文件my_layout.xml非常简单:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/gdi_arrow_up"
android:orientation="vertical"/>
<ImageView
android:id="@+id/gdi_arrow_up"
android:layout_width="27dip"
android:layout_height="27dip"
android:layout_marginLeft="-10dip"
android:scaleType="fitCenter"
android:layout_marginBottom="-8dip"
android:src="?attr/asListArrowUp" />
<ImageView
android:id="@+id/gdi_arrow_down"
android:layout_width="27dip"
android:layout_height="27dip"
android:scaleType="fitCenter"
android:layout_marginBottom="-8dip"
android:layout_below="@android:id/list"/>
</RelativeLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
然后,您可以向popover类添加一个简单的帮助器方法,以将视图(即具有可选侦听器的行)添加到弹出框的主体:
public void addViewToContent(View v, OnClickListener listener) {
content.addView(v);
if (listener != null) {
v.setOnClickListener(listener);
}
}
Run Code Online (Sandbox Code Playgroud)
创建弹出窗口的实例后,您可以通过调用来显示它
menuPopover.show(anchorView);
Run Code Online (Sandbox Code Playgroud)
这是一个稍微简化的版本 - 在实践中,我们将一些附加信息,监听器等附加到这些视图,以使它们在单击时实际执行操作.如果需要,可以访问https://github.com/todoroo/astrid查看完整代码- 该课程为com.todoroo.astrid.ui.MainMenuPopover.
感谢您使用Astrid!
归档时间: |
|
查看次数: |
25531 次 |
最近记录: |