2012-03-12 29 views
5

Làm cách nào để tạo kiểu cho PopupMenu trong Android? tôi muốn có một nền màu xám và một biểu tượng trước văn bản.Kiểu PopupMenu Android

  • tôi có thể sửa lỗi động này trong mã java không.
  • tôi có sửa lỗi này với tệp kiểu toàn cầu không?

như thế này:

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

hoặc i tốt hơn có thể xây dựng một PopupWindow như trong chủ đề này?

inflate popup menu items programatically

Cảm ơn bạn.

Trả lời

2

Tôi không thể tìm thấy cách dễ dàng để tạo kiểu cho PopupMenu của mình vì vậy tôi đã sử dụng "PopupWindow" thay thế, chuyển một listview vào nó và tạo kiểu như tôi muốn.

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

sau đó trên mục danh sách, hãy nhấp vào sử dụng contentopupWindow.dismiss(); để đóng cửa sổ tự động –

Các vấn đề liên quan