【问题标题】:How to open context Menu at specific position in Android and not as menu dialog如何在Android中的特定位置打开上下文菜单而不是菜单对话框
【发布时间】:2023-03-21 16:30:01
【问题描述】:

如何在指定位置打开上下文菜单,如下图所示。该应用程序是 doubleTwistPlayer - 请参阅Play Store

点击它会打开上下文菜单,而不是上下文菜单对话框


已经尝试过

menu_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/itemApplyNew"
        android:title="@string/applyNew"/>

    <item android:id="@+id/itemAppliedLeaves"
        android:title="@string/appliedLeaves"/>    


</menu>

list_item.xml(因为我想在列表中实现这个)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<!-- <RelativeLayout  
    android:id="@+id/relUpperBody"
    android:background="@color/listViewTopContentBackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"> -->
<TextView
    android:id="@+id/txvLeaveName"
    style="@style/ListViewItemHeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"        
    android:text="sdfsdfsd" />

<TextView
    android:id="@+id/txvBalanceLeave"
    style="@style/ListViewItemHeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/imgLeaveAction"
    android:text="dsfgd" />
<!-- </RelativeLayout> -->

<TextView
    android:id="@+id/txvAllottedLbl"
    style="@style/ListViewItemTextSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txvLeaveName"
    android:layout_marginTop="15dp"
    android:text="@string/allotted" />

<TextView
    android:id="@+id/txvAllottedLeave"
    style="@style/ListViewItemTextSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txvBalanceLeave"
    android:layout_alignBaseline="@+id/txvAllottedLbl"
    android:text="sgsdg" />

<ImageView
    android:id="@+id/imgLeaveAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ellipsis_menu_overflow" />


问题
  1. 如何实现?
  2. 是上下文菜单还是其他什么?

【问题讨论】:

  • 好的,我会分享。等待
  • @JordiCastilla 立即查看。
  • youre close, your list layout connot match_parent`,必须更小。在显示时根据调用者给它一个相对位置。

标签: android android-listview android-contextmenu


【解决方案1】:

您需要使用PopupMenu

弹出菜单
弹出菜单显示垂直列表中的项目列表,该列表锚定到调用菜单的视图。这对 提供与特定内容或与 为命令的第二部分提供选项。弹出窗口中的操作 menu 不应该直接影响相应的内容——这就是 上下文动作是为了。相反,弹出菜单用于扩展 与您活动中的内容区域相关的操作。

更多关于实现它here

【讨论】:

  • 感谢您的详细解释和开发链接。
【解决方案2】:

我认为这是一个 ListPopupWindow,您需要做的就是从列表适配器创建一个回调到您的片段/活动,并将回调中的单击视图作为参数传递,并使用 ListPopupWindow 将锚点设置为视图您已经通过了,您可以创建自己的自定义适配器并将其设置为 ListPopupWindow 并传递您想要在弹出窗口中显示的项目列表,希望这会有所帮助。

【讨论】:

    【解决方案3】:

    使用弹出菜单。 如果空间可用,Android 弹出菜单会在锚文本下方显示菜单,否则在锚文本上方。如果您在弹出菜单之外单击,它就会消失。

    检查here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多