2010-04-27 11 views
19

Sto provando a cambiare il grigio chiaro standard in verde chiaro. Sembra che non ci sia un modo semplice per farlo (tramite Android Themes, ad esempio) ma ho trovato una soluzione alternativa come spiegato in questa pagina: http://tinyurl.com/342dgn3.Cambia colore di sfondo del menu di Android

L'autore sembra scomparso, qualcuno può aiutarmi ad integrare questo codice? Non capisco dove devo implementare la classe di fabbrica LayoutInflater.

+0

Se qualcuno è interessato ho risolto. http://pastebin.com/1QHGTMUW chiama semplicemente il setMenuBackground nel onCreate – rciovati

+0

Ecco come ho ottenuto il colore di sfondo del menu della barra delle azioni personalizzato su Android 4.0 e versioni successive: http://stackoverflow.com/a/20077381/56285 – Jonik

+0

provo codice, ma prima dimmi di rimuovere @Override e poi nothings. Ho provato a cambiare il colore di fondo del menù. – Kenji

risposta

10

Quando ur stanno gonfiando la chiamata menu di questo setMenuBackground metodo()

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    MenuInflater inflater=getMenuInflater(); 
    inflater.inflate(R.menu.menu,menu); 
    setMenuBackground(); 
    return true;  
} 

e scrivere questo nel metodo

protected void setMenuBackground(){      
     // Log.d(TAG, "Enterting setMenuBackGround"); 
     getLayoutInflater().setFactory(new Factory() { 
      public View onCreateView(String name, Context context, AttributeSet attrs) { 
       if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { 
        try { // Ask our inflater to create the view 
         LayoutInflater f = getLayoutInflater(); 
         final View view = f.createView(name, null, attrs); 
         /* The background gets refreshed each time a new item is added the options menu. 
         * So each time Android applies the default background we need to set our own 
         * background. This is done using a thread giving the background change as runnable 
         * object */ 
         new Handler().post(new Runnable() { 
          public void run() { 
           // sets the background color 
           view.setBackgroundResource(R.color.androidcolor); 
           // sets the text color    
           ((TextView) view).setTextColor(Color.BLACK); 
           // sets the text size    
           ((TextView) view).setTextSize(18); 
       } 
         }); 
        return view; 
       } 
      catch (InflateException e) {} 
      catch (ClassNotFoundException e) {} 
     } 
     return null; 
    }}); 
} 
+1

Qualcuno ha ottenuto che funzioni? Sembra non avere alcun effetto (Gingerbread) –

+0

Funziona con Android 2.2 non ho verificato con 2.3 –

+1

non funziona su 2.1 qui – melanke

0

Utilizzare setMenuBackground in onCreate.

+0

oops vedo che hai già risposto alla tua stessa domanda. bel lavoro. – locoboy

+0

dove ?? quale OnCreate – user1882196

5

Questo setMenuBackground() è chiaramente un problema che un sacco di programmatori hanno e a cui Google deve ancora fornire una soluzione soddisfacente e supportata.

Il setMenuBackground() hack che Abhay Kumar e Nik Reiman hanno pubblicato è un buon inizio ma si blocca o non funziona su Android 2.3.

Si prega di vedere la mia risposta (Louis Semprini) in questa domanda StackOverflow per un migliore hack commentato e più raffinato che funziona su 2.1, 2.2 e 2.3 (e che non dovrebbe fare danni su 3.X, anche se non possiamo mai garantire questo):

How to change the background color of the options menu?

Inoltre, qui ci sono molte altre risorse che si possono trovare utili per questa domanda:

Change background color of android menu

Android: customize application's menu (e.g background color)

http://www.macadamian.com/blog/post/android_-_theming_the_unthemable/

Android MenuItem Toggle Button

Is it possible to make the Android options menu background non-translucent?

http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx

Setting the menu background to be opaque

Problemi correlati