2017-07-27 74 views
3

我不明白爲什麼如此痛苦改變Android應用程序的顏色。 我嘗試了幾種方法來更改我的操作欄中彈出菜單的背景顏色,但沒有成功。Android更改彈出式菜單的背景顏色

我正在使用AppTheme.NoActionBar風格,顯然,操作欄。

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
    <item name="navigationIcon">@drawable/ic_return_dark</item> 
    <item name="overlapAnchor">false</item> 
    <item name="popupMenuStyle">@style/PopupMenu</item> 
</style> 

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu"> 
    <item name="android:popupBackground">@color/color4</item> 
</style> 

此之後,我發現,有這樣的解釋,你需要在你的自定義樣式插入(在我的情況AppTheme.NoActionBar)的自定義popupMenuStyle,以定製的popupBackground的例子,它改變了彈出式菜單的背景顏色。 它不起作用。

enter image description here

我能做些什麼來改變彈出菜單的背景顏色?

+0

@Pavneet_Singh嘗試並給出此消息:「錯誤:(2688,21)找不到與給定名稱匹配的資源:attr'popupBackground'。」 – sfirc

回答

1

要更改Android中選項菜單的顏色,更改主題和樣式將無濟於事。您必須初始化LayoutInflater Factory Class。

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.my_menu, menu); 
    getLayoutInflater().setFactory(new Factory() { 
    @Override 
    public View onCreateView(String name, Context context, 
    AttributeSet attrs) { 
    if (name .equalsIgnoreCase(「com.android.internal.view.menu.IconMenuItemView」)) { 
    try { 
    LayoutInflater f = getLayoutInflater(); 
    final View view = f.createView(name, null, attrs); 
    new Handler().post(new Runnable() { 
    public void run() { 

    view.setBackgroundResource(R.drawable.your_color); 

    ((TextView) view).setTextColor(Color.your_color); 
    } 
    }); 
    return view; 
    } catch (InflateException e) { 
    } catch (ClassNotFoundException e) { 
    } 
    } 
    return null; 
    } 
    }); 
    return super.onCreateOptionsMenu(menu); 
    } 
+0

我正在爲onCreateOptionsMenu方法中的菜單充氣。這些項目嵌套在xml文件中。我不確定在哪裏使用OverflowMenu風格才能工作。你可以幫我嗎? – sfirc

+0

將代碼添加到styles.XML – Abhi

+0

僅僅在styles.xml中聲明上面的代碼並沒有意義,它不起作用。我應該在哪裏使用OverflowMenu,除styles.xml之外的其他地方? – sfirc