2012-03-12 12 views
5

Come stile un PopupMenu in Android? voglio uno sfondo grigio e un'icona prima del testo.Style PopupMenu Android

  • posso risolvere questo dinamico nel codice java.
  • devo risolvere questo problema con un file di stile globale?

come questo:

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

o posso meglio costruire un PopupWindow come in questa discussione?

inflate popup menu items programatically

Grazie.

risposta

2

Non sono riuscito a trovare un modo semplice per personalizzare il mio PopupMenu, quindi ho usato "PopupWindow", invece di passare una listview ad esso, e lo stile come voglio.

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

quindi sull'elemento dell'elenco fare clic uso contentsopupWindow.dismiss(); per chiudere automaticamente la finestra –

Problemi correlati