我想实现一个自定义弹出菜单,如 Twitter在 Android 中,例如项目和图片,但我不知道用于此的组件是什么。
在 Material Design 网站上,谷歌展示了 this solution .所以我认为,有一个本地解决方案可以实现这一点。
我试过 Popup menu ,但我找不到如何自定义这个 View 的布局。
最佳答案
您可以使用 ListPopupWindow ,提交您的自定义适配器,通过它您可以控制 ListPopupWindow
的每一行的布局。对于普通的 PopupWindow
,你必须提供一个 anchor View ,另外你必须在 ListPopupWindow
的实例上调用 setContentWidth
,它设置宽度弹出窗口的内容大小。这是您必须付出的很小的代价,但对于小型数据集来说并不是什么大问题。我有这个实用方法来检索行的最大宽度:
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;
}
关于android - 如何使用 Material Design Android 实现这个自定义弹出菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184021/