2012-08-27 42 views
0

我創建了一個簡單的菜單,其中有兩個選項,「添加新聯繫人」&「設置」與白色PNG圖像。安卓選項菜單問題在不同版本

所以,現在,如果我在2.3.3 Android操作系統版本上運行,它看起來像下面的圖片:

Android 2.3.3 option menu

現在如果我在2.2 Android操作系統運行,那麼它看起來像下面的圖片

enter image description here

所以,現在我能做些什麼,如果我想使背景黑色的Android 2.2,這樣我可以得到圖標可見。 請給我任何關於這個問題的建議。

+0

您可以自定義選項菜單選項卡,並提供自己的背景顏色/圖片。 – Nishant

+0

那我該如何設置菜單的背景? –

+3

看看這個http://stackoverflow.com/questions/2944244/change-the-background-color-of-the-options-menu – Nishant

回答

1

處理選項菜單的最佳方法是自定義它。

您可以自定義選項菜單,其中包括:

添加自定義字體

更改字體大小

更改字體顏色

設置背景的繪製資源(例如圖像,邊界,漸變)

要將背景改變爲邊框或漸變,您必須在res中創建資源文件夾,稱爲繪製能夠在內部創建邊界XML或漸變XML。

這都可以通過程序來完成,如下圖所示:

public class CustomMenu extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  } 
public boolean onCreateOptionsMenu(android.view.Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.cool_menu, menu); 
    getLayoutInflater().setFactory(new Factory() { 
     public View onCreateView(String name, Context context, 
       AttributeSet attrs) { 
     if (name.equalsIgnoreCase(           "com.android.internal.view.menu.IconMenuItemView")) { 
    try { 
     LayoutInflater li = LayoutInflater.from(context); 
     final View view = li.createView(name, null, attrs); 
     new Handler().post(new Runnable() { 
     public void run() { 
// set the background drawable if you want that or keep it default 
//either FOR image, border, gradient, drawable,etc.// 
     view.setBackgroundResource(R.drawable.myimage); 
     ((TextView) view).setTextSize(20); 
     // set the text font and color 
     Typeface face = Typeface.createFromAsset( 
     getAssets(),"OldeEnglish.ttf"); 
     ((TextView) view).setTypeface(face); 
     ((TextView) view).setTextColor(Color.RED);  }  }); 
     return view;  } 
     catch (InflateException e) { 
     //Handle any inflation exception here 
     } catch (ClassNotFoundException e) {  
     //Handle any ClassNotFoundException here  
         }  } 
      return null; } }); 
      return super.onCreateOptionsMenu(menu);  } 
1

您只需要通過此鏈接here我面臨同樣的問題,並在那裏找到答案。

+0

我也從這個鏈接得到了解決方案。在這個鏈接看起來很好的討論。 –