【问题标题】:Style PopupMenu Android样式 PopupMenu Android
【发布时间】:2012-03-29 03:17:57
【问题描述】:

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

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

像这样:

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

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

inflate popup menu items programatically

谢谢。

【问题讨论】:

    标签: android xml styles popupmenu


    【解决方案1】:

    我找不到一种简单的方法来设置我的 PopupMenu 的样式,所以我改用“PopupWindow”,将一个列表视图传递给它,然后按照我的需要设置它的样式。

    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); 
    

    【讨论】:

    • 然后在列表项上单击使用 contentsopupWindow.dismiss();自动关闭窗口
    猜你喜欢
    • 2017-04-03
    • 2012-07-28
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2015-01-23
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多