如何实现与材料设计的Andr​​oid这个自定义的弹出式菜单?弹出式、自定义、如何实现、菜单

由网友(披星戴月.)分享简介:我想实现像一个自定义的弹出式菜单Twitter在Android中,例如与项目和图片,但我不知道什么是用于该组件。 I want to realize a custom popup menu like Twitter in Android for example with item and picture but I...

我想实现像一个自定义的弹出式菜单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.

推荐答案

您可以使用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;
}
阅读全文

相关推荐

最新文章