[英]How to change background color of overflow menu (popup Menu) for action bar
[英]How to set a custom color for the popup of action items, including of the overflow menu?
我正在為應用添加一些材質設計風格,所以我為動作欄和狀態欄選擇了不同的顏色。
為此,該應用程序的主題是“Theme.AppCompat.Light.DarkActionBar”,並添加此以隱藏操作欄,因為我需要將其作為工具欄處理:
我用過的主題:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">true</item>
</style>
布局我用過:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/activity_app_list__toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:ignore="UnusedAttribute"/>
</LinearLayout>
碼:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_toolbar=(Toolbar)findViewById(R.id.activity_app_list__toolbar);
setSupportActionBar(_toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
由於某種原因,我得到這種行為:
我想自定義這些彈出菜單,以便它們保持一致。
我試過用這個:
<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
...
<style name="OverflowMenu" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
</style>
但它根本沒有幫助。
有誰知道如何處理這個? 這是支持庫中的錯誤嗎?
對我來說它看起來像這樣,所以我在這里報告了它,包括一個示例項目。
您可以使用popupTheme
屬性自定義溢出菜單:
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/triple_height_toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
原來的答案缺少一些觀點:
首先,工具欄應該具有:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"/>
對於輕彈出,請使用:
<style name="AppTheme.Light" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarTheme">@style/AppTheme.ActionBarTheme.Light</item>
<item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>
<style name="AppTheme.ActionBarTheme.Light" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlActivated">#FFffffff</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
</style>
對於黑暗彈出窗口,請使用:
<style name="AppTheme.Light" parent="@style/Theme.AppCompat.NoActionBar">
<item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Dark</item>
<item name="actionBarTheme">@style/AppTheme.ActionBarTheme.Dark</item>
</style>
<style name="AppTheme.ActionBarTheme.Dark" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlActivated">#FFffffff</item>
<item name="android:textColorPrimary">#FFffffff</item>
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.