android - Android 风格的 PopupMenu

标签 android xml styles popupmenu

我如何在 android 中设置 PopupMenu 的样式?我想要灰色背景和文本前的图标。

  • 我可以在 java 代码中修复这个动态吗?
  • 我要用全局样式文件解决这个问题吗?

像这样:

<style name="GreenText" parent="@android:style/PopupMenu">
    <item name="android:textColor">#00FF00</item>
</style>

或者我可以像在这个线程中那样更好地构建一个 PopupWindow 吗?

inflate popup menu items programatically

谢谢。

最佳答案

我找不到一种简单的方法来设置 PopupMenu 的样式,所以我改用“PopupWindow”,将 ListView 传递给它,然后根据需要设置样式。

popView=layoutInflater.inflate(R.layout.pop_layout, null); // layout with a listview to     put in the popup window
lv=(ListView)popView.findViewById(R.id.pop_listview); // then set listview parameters

final PopupWindow contentsopupWindow = new PopupWindow(popView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
           contentsopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pixel_dot));// set a background so the menu disappears when you click outside it
          contentsopupWindow.setTouchable(true);
          contentsopupWindow.setFocusable(true);
          contentsopupWindow.setOutsideTouchable(true);
          contentsopupWindow.setTouchInterceptor(new OnTouchListener() {
           public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        contentsopupWindow.dismiss();
                        return true;
                        }
                        return false;
                        }
            });
           WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);


            contentsopupWindow.showAsDropDown(anchorView); 

关于android - Android 风格的 PopupMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9671896/

相关文章:

android - 无法在 android 中从 Fragment 运行 ShowcaseView

java - 传入和传出蓝牙连接的并发线程

xml - 使用sed从XML文件中删除节点和数据

html - 将文本转换为大写会干扰布局

Android - 是否可以将相机的预览帧速率设置为 'lock'?

android - RecyclerView GridLayoutManager : how to auto-detect span count?

java - Struts 2 中重定向到另一个参数数量未知的操作

xml - 在 shell 脚本中使用 sed 命令在 XML 文件中添加 XML 元素

javascript - this.style.display = 无; Google Chrome 中的问题 - HTML5

css - 子 CSS 类(两个 slider 但需要不同的 css 规则)