我搜索了很多网站,但它们都已过时。如何在 Android 8.0 中创建全屏浮动上下文菜单?
正如您在此处看到的,Google 在其指南中为我们提供了如何操作的说明,但我想它们也已过时。为什么?下面是一个例子:
这就是 API 23 上的样子(来源,他的 github 代码说它是 API 23 = Android 6.0)

这就是它在 API 26 (Android 8.0) 上的样子:
这些 API 之间肯定有一些变化,但我无法真正得到答案。
任何解决方案将不胜感激。
这实际上是特定于版本的行为。
您链接的项目的解决方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
registerForContextMenu(button);
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
button.showContextMenu();
return true;
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果阅读有关View类的performLongClick()的文档:
/**
* Calls this view's OnLongClickListener, if it is defined. Invokes the
* context menu if the OnLongClickListener did not consume the event,
* anchoring it to an (x,y) coordinate.
*
* @param x x coordinate of the anchoring touch event, or {@link Float#NaN}
* to disable anchoring
* @param y y coordinate of the anchoring touch event, or {@link Float#NaN}
* to disable anchoring
* @return {@code true} if one of the above receivers consumed the event,
* {@code false} otherwise
*/
public boolean performLongClick(float x, float y)
Run Code Online (Sandbox Code Playgroud)
所以它是显示上下文菜单的标准实现(如果您在视图上注册侦听器上下文菜单,它将使用坐标进行处理)。因此,为了避免显示带有坐标的菜单,只需通过showContextMenu()长按视图即可直接显示
| 归档时间: |
|
| 查看次数: |
1164 次 |
| 最近记录: |