如何实现与材料设计的Andr​​oid这个自定义的弹出式菜单? [英] How to realize this custom popup menu with Material Design Android?

查看:99
本文介绍了如何实现与材料设计的Andr​​oid这个自定义的弹出式菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现像一个自定义的弹出式菜单<一href="https://play.google.com/store/apps/details?id=com.twitter.android">Twitter在Android中,例如与项目和图片,但我不知道什么是用于该组件。

I want to realize a custom popup menu like Twitter in Android for example with item and picture but I don't know what's the component used for that.

在材料设计的网站,谷歌present 该解决方案。因此,我认为,有一个本地的解决方案来实现这一目标。

In Material Design website, google present this solution. So I think, there is a native solution to achieve this.

我试着用 弹出菜单 ,但我怎么也找不到自定义此观点类似的布局。

I tried with Popup menu, but I can't find how to customize the layout of this view like that.

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/support/v7/widget/ListPopupWindow.html">ListPopupWindow,提交您的自定义接口,通过它可以控制 ListPopupWindow 的每一行的布局。作为一个正常的 PopupWindow 你必须提供一个锚视图,另外你得叫 setContentWidth 上的实例 ListPopupWindow ,它通过其内容的大小设置弹出窗口的宽度。这是一个很小的代价,你必须付出,但对于小数据集是不是一个大问题。我有这样的工具方法来获取最大宽度的行:

you can use a ListPopupWindow, submitting your custom adapter, through which you can control the layout of every single row of the ListPopupWindow. As for a normal PopupWindow you have to provide an anchor view and additionally you have to call setContentWidth on the instance of ListPopupWindow, which sets the width of the popup window by the size of its content. It is a small price you have to pay, but for a small dataset is not a big deal. I have this utility method to retrieve the max width of the row:

public int measureContentWidth(ListAdapter adapter) {
    int maxWidth = 0;
    int count = adapter.getCount();
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    View itemView = null;
    for (int i = 0; i < count; i++) {
        itemView = adapter.getView(i, itemView, this);
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        maxWidth = Math.max(maxWidth, itemView.getMeasuredWidth());
    }
    return maxWidth;
}

这篇关于如何实现与材料设计的Andr​​oid这个自定义的弹出式菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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