样式PopupMenu Android

Mit*_*tch 5 xml android styles popupmenu

我如何设计Android中的PopupMenu?我想要一个灰色的背景和文本前的图标.

  • 我能在java代码中修复这个动态吗?
  • 我用全局样式文件修复此问题?

像这样:

<style name="GreenText" parent="@android:style/PopupMenu">
    <item name="android:textColor">#00FF00</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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

以编程方式膨胀弹出菜单项

谢谢.

Bas*_*ssa 2

我找不到一种简单的方法来设计 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); 
Run Code Online (Sandbox Code Playgroud)