2015-04-04 41 views
1

我有以下操作菜單:如何刪除操作菜單項的背景?

enter image description here

正如你可以看到所有這些行動的菜單項都具有 背景。我想這是像父母一樣的背景。

如何刪除此背景?

我style.xml如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> 

<style name="AppTheme" parent="AppTheme.Base"></style> 

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimaryDark">@color/primaryColorDark</item> 
    <item name="colorPrimary">@color/primaryColor</item> 
    <item name="colorAccent">@color/accentColor</item> 
    <item name="android:textColorPrimary">@color/textcolorsecundary</item> 
    <item name="android:textColorSecondary">@color/textcolorsecundary</item> 
    <item name="android:actionModeBackground">@color/primaryColor</item> 
    <item name="android:windowActionModeOverlay">true</item> 
    <item name="android:activatedBackgroundIndicator">@drawable/custom_background_listview_item</item> 
</style> 

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <item name="android:textColorPrimary">@color/textcolorsecundary</item> 
    <item name="actionMenuTextColor">@color/textcolorsecundary</item> 
    <item name="android:textColorSecondary">@color/textcolorsecundary</item> 
    <item name="android:showDividers">beginning</item> 
    <item name="android:divider">@color/primaryColorDark</item> 
    <item name="android:background">@drawable/custom_background_blue</item> 
</style> 

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

</resources> 

我的工具欄:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="?android:attr/actionBarSize" 
android:elevation="2dp" 
android:focusable="false" 
app:popupTheme="@style/AppTheme.Toolbar.PopupMenu" 
app:theme="@style/AppTheme.Toolbar" > 

<TextView 
    android:id="@+id/toolbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="start" 
    android:background="@null" 
    android:textColor="@color/white" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

</android.support.v7.widget.Toolbar> 
+0

把你的佈局代碼 – 2015-04-04 17:56:11

+0

我添加styles.xml – Mulgard 2015-04-04 17:58:07

回答

2

下面一行是問題

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <item name="android:background">@drawable/custom_background_blue</item> 
</style> 

由於上述應用並不像風格,但作爲一個主題(您Toolbar元素可能通過popupTheme屬性),所有的工具欄裏面的小部件繼承這些設置。你想要的是android:popupBackground而不是android:background

編輯:OP聲明以上是不夠的。對於造型工具欄的彈出菜單(不是工具欄本身),請使用app:popupTheme屬性。主題將與此類似:

<style name="AppTheme.Toolbar.PopupMenu" parent="ThemeOverlay.AppCompat.Dark"> 
    <item name="android:popupBackground">@drawable/custom_background_blue</item> 
</style> 
+0

我沒有在我的工具欄元素上設置popupTheme屬性(我在我的帖子中發佈了工具欄代碼)並且沒有...不幸的是設置了android:popupBackground = @null並沒有幫助解決問題 – Mulgard 2015-04-04 18:22:21

+0

我看到了,我更新了答案,它現在應該工作。 – 2015-04-04 19:08:30

+0

我真的很感謝你的回答,但它再次沒有工作。我添加了'app:popupTheme =「@ style/AppTheme.Toolbar.PopupMenu'我的工具欄,並將'android:popupBackground'設置爲紅色以測試它是否有效。不幸的是,我得到的結果與您在圖片中看到的相同OP – Mulgard 2015-04-04 19:12:29

0

你明確地設定操作的背景顏色在您的style.xml一樣的原色。

<item name="android:actionModeBackground">@color/primaryColor</item> 

嘗試從您的XML中刪除它或將其更改爲適合您的UI的顏色,它應該可以工作。如果你不選擇手動指定android:actionModeBackground的顏色,它會簡單地從父主題派生它的默認值(在你的情況下,Theme.AppCompat.Light.NoActionBar)。

+0

不,這不會改變任何東西。 'actionModeBackground'從未起作用,因爲我將我的工具欄樣式設置爲上面發佈的'AppTheme.Toolbar'。我在那裏定義的分隔線也沒有出現。 – Mulgard 2015-04-04 18:11:19

+0

爲什麼不直接支持Action Bar(Light or Dark,無論你想要什麼)的主題,然後只爲你關心的界面元素指定顏色? – 2015-04-04 18:14:42

+0

是不是在做什麼?我的設計師關心標題欄和彈出式菜單。是否很難簡單地改變這些溢出菜單項的背景? – Mulgard 2015-04-04 18:18:00