自定义弹出菜单(布局)

2023-12-23

我正在尝试升级我的 PopupMenu,以便它带有图标和自定义样式。
我为它创建了一个新的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/layout_sharea"
        android:background="@drawable/share"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip"
        android:layout_width="wrap_content"
        android:layout_height="50.0dip"
        android:onClick="share">
        <TextView
            android:id="@+id/sharetexta"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:drawableLeft="@drawable/share_button"
            android:drawablePadding="10.0dip"
            android:layout_centerVertical="true" />
     </RelativeLayout>
     <RelativeLayout
        android:id="@+id/layout_shareb"
        android:background="@drawable/share"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip"
        android:layout_width="wrap_content"
        android:layout_height="50.0dip"
        android:layout_below="@+id/layout_sharea"
        android:onClick="share">
        <TextView
            android:id="@+id/sharetextb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:drawableLeft="@drawable/share_button"
            android:drawablePadding="10.0dip"
            android:layout_centerVertical="true" />
     </RelativeLayout>
</RelativeLayout>

I want the PopupMenu to be customized (to be this layout) like in this picture PopupMenu


A 弹出菜单 http://developer.android.com/reference/android/widget/PopupMenu.html是为了显示Menus http://developer.android.com/reference/android/view/Menu.html并且确实没有一个好的方法来自定义菜单项的外观。如果你想要更灵活的东西,你的答案是列表弹出窗口 http://developer.android.com/reference/android/widget/ListPopupWindow.html.

private static final String TITLE = "title";
private static final String ICON = "icon";

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title, int iconResourceId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(TITLE, title);
    map.put(ICON, iconResourceId);
    data.add(map);
}

// Call this when you want to show the ListPopupWindow
private void showListMenu(View anchor) {
    ListPopupWindow popupWindow = new ListPopupWindow(this);

    ListAdapter adapter = new SimpleAdapter(
            this,
            data,
            android.R.layout.activity_list_item, // You may want to use your own cool layout
            new String[] {TITLE, ICON}, // These are just the keys that the data uses
            new int[] {android.R.id.text1, android.R.id.icon}); // The view ids to map the data to


    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource
    popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected
    popupWindow.show();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义弹出菜单(布局) 的相关文章

随机推荐

  • 在文本框中的最后一个字符后设置焦点

    我有 3 个电话号码文本框 当用户键入时 它会自动从一个文本框移动到下一个文本框 当用户按退格键时 我可以将焦点移动到上一个文本框 问题是在 IE 中 焦点设置在文本框的开头 这是我的代码 在 Chrome 中运行良好 AreaCode l
  • 使用 Response.TransmitFile 下载文件但也包含页面源

    下面是有问题的代码 我下载了一个 csv 但是它将页面源附加到底部 关于如何防止这种情况有什么想法吗 var priceList Test const string downloadName PriceList csv var fs new
  • 使文件夹不受 SVN 管理

    是否可以从存储库中删除该文件夹 就像不会从每个用户的本地存储库中删除该文件夹一样 就我而言 有一个名为 config 的目录 旨在进行版本控制 现在我们决定从版本控制中删除该目录 但将其保留在每台计算机上 顺便将其添加到 svn ignor
  • 页面加载完成后图像消失

    我使用以下代码在页脚中显示图像 我看到图像几秒钟 但是页面完全加载后 图像消失了 有人知道这里出了什么问题吗 我正在使用 Rails 3 2 8 和 Chrome 在 Firefox 或 Safari 中不会发生这种情况 Thanks UP
  • 如何编写使用内置相机拍照的 Solo/Robotium 测试用例?

    从我的活动中 我执行 startActivityForResult MediaStore ACTION IMAGE CAPTURE 然后我进入内置相机活动 在本例中是在模拟器中 当我现在这样做时 独奏 clickOnButton 0 在我的
  • 单调的堆栈和队列。定义和例子

    到底什么是单调堆栈 例如 它与单调队列有何不同 例如 考虑以下整数数组 0 2 1 3 4 如果我从左到右处理这个数组并将其插入到单调递减的堆栈中 我应该在堆栈中看到什么 为什么 Here http www leetcode solutio
  • Nginx 具有不同根的多个位置

    我的 nginx 配置非常简单 里面有 3 个位置 他们每个人都有自己的根目录 我将来应该能够轻松添加另一个根目录 我想要的是 Request admin gt 位置 admin Request admin gt 位置 admin Requ
  • 无法将不可变值作为 inout 参数传递:函数调用返回不可变值

    我分叉了这个项目 所以我不太熟悉所有细节 https github com nebs hello bluetooth blob master HelloBluetooth NSData 2BInt8 swift https github c
  • 如何使数据类更好地与 __slots__ 配合使用?

    It 已决定 https github com ericvsmith dataclasses issues 28删除对的直接支持 slots 来自 Python 3 7 的数据类 尽管如此 slots 仍然可以与数据类一起使用 from d
  • 将 TextField 绑定到 ReadOnlyDoubleProperty

    我可以绑定一个TextField的文本属性为DoubleProperty 像这样 textField textProperty bindBidirectional someDoubleProperty new NumberStringCon
  • 将数据插入 SQL Server 返回错误代码 00q

    首先 我的代码的最大部分是在几周前工作的 与此同时 我们从 MySQL 服务器更换为 SQL 服务器 并且对架构设计进行了一些更改 我使用 Percobol 据我所知 它使用 OpenCobol 编译器 PERFORM 2010 GEBRU
  • Dynamic_cast<> 是否仅限于沿着继承层次结构进行直接转换?

    CODE struct A virtual details there but left out struct B virtual details there but left out struct C A B virtual detail
  • WaitHandle.WaitAll 64 个句柄限制的解决方法?

    我的应用程序通过以下方式生成不同的小工作线程负载ThreadPool QueueUserWorkItem我通过多个跟踪ManualResetEvent实例 我用WaitHandle WaitAll方法阻止我的应用程序关闭 直到这些线程完成
  • 如何向 RCpp 中的数据框添加新列?

    我正在尝试使用 RCpp 向数据框添加新列 在下面的代码中 我打算向数据框 df 添加一个 结果 列 但运行代码后数据集没有 结果 列 你能告诉我他们出了什么问题吗 R 文件调用 AddNewCol 函数 library Rcpp sour
  • 如何在 Perl 中将数组转换为散列?

    我有一个数组 并尝试将数组内容转换为带有键和值的哈希值 索引 0 是键 索引 1 是值 索引 2 是键 索引 3 是值 依此类推 但它没有产生预期的结果 代码如下 open FILE message xml die Cannot open
  • 初始拒绝后使用 getUserMedia() 重新提示权限

    在被拒绝一次后 我们如何使用 getUserMedia 请求相机 麦克风访问权限 我正在使用 getUserMedia 来访问用户的相机并将数据通过管道传输到画布 那一点一切都很好 在测试中 我点击了一次拒绝 此时 在 Chrome 和 F
  • 如何伪造Time.now?

    最好的设置方法是什么Time now为了在单元测试中测试时间敏感的方法 我真的很喜欢Timecop https github com travisjeffery timecop图书馆 您可以以块形式进行时间扭曲 就像时间扭曲一样 Timec
  • 如何从特定模块创建所有装饰函数的向量?

    我有一个文件main rs和一个文件rule rs 我想定义函数rule rs将被纳入Rules rule矢量 而不必将它们一一推动 我更喜欢一个循环来推动它们 main rs struct Rules rule Vec
  • 错误 1054。Insert 子句中的未知列

    我有这个问题 如果我编写以下查询 INSERT INTO prodotto Barcode InseritoDa DataInserimento UrlImage VALUES vfr ff 12 10 2012 vfr jpg 我收到此错
  • 自定义弹出菜单(布局)

    我正在尝试升级我的 PopupMenu 以便它带有图标和自定义样式 我为它创建了一个新的布局