3

注:我已經尋找了一個小時,並試圖通過已經提供的計算器所有 解決方案。改變彈出菜單的背景顏色

我正在學習主題疊加。我製作了一個示例應用程序,在點擊一個操作欄圖標時打開一個彈出式菜單。這裏是我的styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 

    </style> 

    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 



    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light"> 
     <item name="android:textColorPrimary">@color/colorAccent</item> 
    </style> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark"> 
     <!-- added all to see which one will work.--> 
     <item name="android:popupMenuStyle">@style/PopupMenu</item> 
     <item name="android:itemBackground">@color/colorAccent</item> 
     <item name="android:colorBackground">@color/colorAccent</item> 

    </style> 

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

</resources> 

這裏是我的工具欄樣式。

<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    </android.support.design.widget.AppBarLayout> 

我已經設置了popupTheme到一個我在我的styles.xml。現在我想更改彈出式菜單的背景色,該菜單當前是白色的。

enter image description here

這裏是代碼。

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if(item.getItemId() == R.id.standard_menu){ 
      showPopupMenu(item); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void showPopupMenu(MenuItem item) { 
     PopupMenu p = new PopupMenu(this, findViewById(item.getItemId())); 
     p.inflate(R.menu.pop_menu); 
     p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
       Toast.makeText(MainActivity.this, "clicked.", Toast.LENGTH_SHORT).show(); 
       return true; 
      } 
     }); 
     p.show(); 
    } 
+0

您是否嘗試過'<項目名稱= 「popupMenuStyle」> @風格/ PopupMenu的'? – Karakuri

+0

是的。不起作用。 – mallaudin

+0

請看這裏:http://www.silverbaytech.com/2013/05/27/themes-for-the-android-actionbar-actionbaritems/ –

回答

15

我沒」不滿足於接受的答案,因爲它並不能真正解釋爲什麼OPs自定義彈出式樣式沒有被應用 - 不僅僅是背景,而且還有像第電子文本的顏色 - 所以我做了我自己的實驗。

重要的是要注意,由Toolbar創建的彈出窗口(當它具有菜單項時)與PopupMenu自己顯示的窗口之間存在差異。這些受不同的主題屬性支配。此外,請注意有兩個PopupMenu分類:android.widget.PopupMenuandroid.support.v7.widget.PopupMenu

您明確顯示的需要樣式PopupMenu的主題屬性是android:popupMenuStylepopupMenuStyle。你有幾個選項來實現您的自定義樣式正確應用:

(1)使用android:popupMenuStyle在活動(或應用)

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- if using android.widget.PopupMenu --> 
    <item name="android:popupMenuStyle">@style/PopupMenu</item> 
    <!-- if using android.support.v7.widget.PopupMenu --> 
    <item name="popupMenuStyle">@style/PopupMenu</item> 
</style/> 

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

PopupMenu popup = new PopupMenu(this, anchorView); 

注意的主題,這需要任何額外的佈局文件。

(2)使用ContextThemeWrapper

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- nothing special --> 
</style/> 

<style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark"> 
    <!-- if using android.widget.PopupMenu --> 
    <item name="android:popupMenuStyle">@style/PopupMenu</item> 
    <!-- if using android.support.v7.widget.PopupMenu --> 
    <item name="popupMenuStyle">@style/PopupMenu</item> 
</style> 

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

ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomPopupTheme); 
PopupMenu popup = new PopupMenu(ctw, anchorView); 

注意如何構建ContextThemeWrapper時不直接使用R.style.PopupMenu。這看起來有點迂迴,但如果要將彈出式主題與活動或應用程序主題分開(例如,可能只有某些彈出窗口需要特殊主題),這很有用。

(3)使用您AppBarLayout的語境

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- nothing special --> 
</style/> 

<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Light"> 
    <!-- if using android.widget.PopupMenu --> 
    <item name="android:popupMenuStyle">@style/PopupMenu</item> 
    <!-- if using android.support.v7.widget.PopupMenu --> 
    <item name="popupMenuStyle">@style/PopupMenu</item> 
</style> 

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

<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Dark"> 
    <!-- changes the background of the Toolbar's popup --> 
    <item name="android:colorBackground">@color/popupBackground</item> 
</style> 


<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/PopupOverlay"/> 

</android.support.design.widget.AppBarLayout> 


AppBarLayout appBar = (AppBarLayout) findViewById(R.id.app_bar); 
PopupMenu popup = new PopupMenu(appBar.getContext(), anchorView); 

既然你已經爲AppBar一個主題覆蓋,你可以用它來保存您的彈出式主題引用。這也適用於工具欄的上下文,至少在給出當前佈局的情況下,儘管請注意app:popupTheme在這裏並不實際相關,因爲它會影響Toolbar的彈出框而不是PopupMenu。還要注意的是如何相似這是上面的選項2,應該線索,你到android:theme屬性引擎蓋下是如何工作的;)

在我的實驗,android:itemBackground當我在PopupOverlay風格用它代替android:colorBackground只工作。但是,最好使用android:colorBackground,因爲這會更改彈出窗口的顏色,使項目的圓角和可選項目突出顯示/紋波保持不變。

+0

感謝您的詳細解答。我會應用它,並會接受你的答案,如果它的工作。 – mallaudin

+0

完美!搞定了。 – mallaudin

+0

選項2:適合我! – Nick

0

添加彈出菜單樣式UR AppTheme:

<style name="AppTheme" parent="android:Theme.Light"> 
    <item name="android:popupMenuStyle">@style/PopupMenu</item> 
</style> 

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

的manifest.xml:

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
............. 
</application> 

我希望它會工作。

+0

你沒有看到評論嗎?我已經嘗試過了。 – mallaudin

1

這爲我工作

<item name="android:itemBackground">@color/primary</item> 

請插入到你的主風格 我希望這對你的作品

+0

是的。它有效,但爲什麼其他選項不起作用? – mallaudin

+1

我很困惑。 OP中的圖像表明整個彈出式樣式未被應用。 'PopupOverlay'擴展了'ThemeOverlay.AppCompat.Dark',所以文字顏色應該很淺,但顯然不是。這可能會解決彈出窗口的背景,但並沒有真正回答爲什麼沒有應用定義的自定義樣式。 – Karakuri

0

嘗試使用colorPrimary內PopupOverlay這樣

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" > 
    <item name="colorPrimary">@color/blue_ivy</item> 
</style> 
+0

不可以。不起作用。 – mallaudin

1

使用 「popupMenuStyle」 爲PopupMenu的支持V7庫,並 使用 「機器人:popupMenuStyle」 定期PopupMenu的