- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看到正式不可能有一个具有自定义 View 甚至行图标的上下文菜单 ( here ),我决定创建我自己的解决方案(具有类似它的自定义 View )。
在 RecyclerView 上使用上下文菜单时,触摸位置很重要,因此如果您长时间触摸某个项目,上下文菜单将尝试出现在触摸位置附近(从 here 中获取的示例),而无需我提供此信息(意思是通过 OnClickListener 或 onLongClickListener ):
但是,我在更基础的类(class)中找不到如何做到这一点。
可以通过长按来显示 PopupWindow,如下所示:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(context)
val holder = ViewHolder(inflater.inflate(R.layout.list_item_main, parent, false))
holder.itemView.setOnLongClickListener {
val contextMenuView=inflater.inflate(R.layout.context_menu,null)
val popupWindow = PopupWindow(contextMenuView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)
popupWindow.showAsDropDown(holder.itemView,0,0);
true
}
return holder
}
而且,如果你想要一个漂亮的背景而不是透明的,你可以使用 ListPopupWindow 的变通方法,如果你不想要一个列表,你可以设置它的 promptView ,这样(代码可用 here ) :
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(context)
val holder = ViewHolder(inflater.inflate(R.layout.list_item_main, parent, false))
val maxAllowedPopupWidth = context.resources.displayMetrics.widthPixels * 90 / 100
holder.itemView.setOnLongClickListener {
val contextMenuView = inflater.inflate(R.layout.context_menu, null)
val listPopupWindow = ListPopupWindow(context)
contextMenuView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val width = min(maxAllowedPopupWidth, contextMenuView.measuredWidth)
listPopupWindow.setPromptView(contextMenuView)
listPopupWindow.setContentWidth(width)
listPopupWindow.anchorView = it
listPopupWindow.show()
true
}
return holder
}
我不确定我计算出的最大宽度,因为我找不到弹出窗口的最大尺寸。我知道上下文菜单有一些最大值,然后出于某种原因它只是截断了文本。也许它和 Dialog 一样?除了对话框,我可以找到最大宽度,但我找到了最小宽度:windowMinWidthMajor
和 windowMinWidthMinor
。
但回到问题:我在这里找不到任何与将弹出窗口放在触摸位置附近相关的功能。
这就是我得到的,例如:
如何将弹出窗口设置为出现在屏幕上的触摸位置附近,甚至不处理 onTouch 事件,就像在使用 ContextMenu 的示例中所做的那样?
上下文菜单(或类似菜单)是否有一些我可以获得的属性,以设置为我显示的最大尺寸(简而言之:默认最大宽度)?如果是这样,我该如何使用它?如何设置宽度和高度以考虑膨胀 View 之一?
最佳答案
我已经有一段时间没有这样做了,但我认为我们遇到了同样的问题。让我看看我能不能回答。
无法为 EditText 制作自定义上下文菜单是我最终决定创建 a library with custom components for Mongolian 的主要原因之一。 .虽然垂直的蒙古语部分对您没有用,但其他自定义弹出窗口的概念应该相同。
这是我所拥有的几个屏幕截图:
这是一个使用自定义弹出菜单的自定义 EditText。它需要用户触摸位置来放置弹出位置。
next one是设置弹出窗口位置的不同方法的更一般的演示。
这两个演示都包含在 mongol-library demo app 中.
我的自定义菜单是 PopupWindow 的子类。你可以找到源代码 here .
我把它放在特定位置的方法是使用 showAtLocation
方法,我记得这只是 PopupWindow 上的一个普通方法:
private void showMongolContextMenu(MongolMenu menu, int xTouchLocation, int yTouchLocation) {
float paddingPx = CONTEXT_MENU_TOUCH_PADDING_DP * getResources().getDisplayMetrics().density;
Rect menuSize = menu.getDesiredSize();
int y = yTouchLocation - menuSize.height() - (int) paddingPx;
menu.showAtLocation(this, Gravity.NO_GRAVITY, xTouchLocation, y);
}
该代码来自here .
哦,是的,我也在自定义键盘中使用了它:
有关更多信息,请参阅这些类:
关于android - 如何在触摸位置附近显示自定义弹出窗口,就像我们使用 ContextMenu 时那样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57290574/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!