自纱厂/下拉菜单纱厂、菜单

2023-09-12 08:31:22 作者:疯癫

在该应用阿斯特丽德任务,有一个按钮。当你preSS按钮,下拉菜单出现。

In the app Astrid Tasks, there is a button. When you press the button, a drop down menu comes up.

这基本上是一个微调,但在下拉列表的形式。

It's basically a spinner but in a drop-down-list form.

有谁知道如何做同样的事情?这是一个小部件我只是不明白?

Does anyone know how to do something similar? Is this a widget I just don't see?

推荐答案

本作的原创作者(我的小学Android开发的阿斯特丽德一个)我会很高兴与大家分享阿斯特丽德是怎么做的。我会后的基础在这里,但你会发现在我们的GitHub库的详细信息( https://github.com/todoroo/astrid)。的基本思想是扩展GreenDroid的QuickActionWidget作为hanry暗示。子类看起来是这样的:

As the original author of this (I'm one of the primary Android developers for Astrid) I'd be happy to share how Astrid does it. I'll post the basics here, but you can find more details at our github repo (https://github.com/todoroo/astrid). The basic idea is to extend GreenDroid's QuickActionWidget as hanry suggests. The subclass looks something like:

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);
    }
}

布局文件my_layout.xml是pretty的简单:

The layout file my_layout.xml is pretty simple:

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

然后,你可以添加一个简单的辅助方法来将popover类添加视图(即行,可选听众)到popover的主体:

Then, you can just add a simple helper method to the popover class to add views (i.e. rows, with optional listeners) to the main body of the popover:

public void addViewToContent(View v, OnClickListener listener) {
    content.addView(v);
    if (listener != null) {
        v.setOnClickListener(listener);
    }
}

创建弹出的一个实例后,可以通过调用显示它

After creating an instance of the popup, you can show it by calling

menuPopover.show(anchorView);

这是一个有点简化版本 - 在实践中,我们附加一些附加信息,监听器等,这些意见,使他们实际上做的事情单击时。如果你愿意,你可以检查出完整的code在 https://github.com/todoroo/astrid - 类是com.todoroo.astrid.ui.MainMenuPopover。

This is a somewhat simplified version -- in practice, we attach some addition information, listeners, etc. to those views to make them actually do things when clicked. If you want, you can check out the full code at https://github.com/todoroo/astrid -- the class is com.todoroo.astrid.ui.MainMenuPopover.

感谢您使用阿斯特丽德!

Thanks for using Astrid!