弹出菜单背景颜色更改不起作用
popup menu background color change not working
我是 android 的初学者。我想弹出菜单背景颜色 change.I 尝试使用此代码更改背景颜色,但它不适用于 me.i 不知道为什么不使用此代码。
<!--popup menu item style-->
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light">
<item name="android:textColor">@color/colorWhite</item>
<item name="android:popupBackground">@color/colorBlack</item>
<item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
<item name="android:divider">@color/colorWhite</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:popupBackground">@color/colorBlack</item>
<item name="overlapAnchor">false</item>
<item name="android:divider">@color/colorRegisterHeader</item>
<item name="android:dividerHeight">1dp</item>
</style>
//这个activity弹出菜单的代码
PopupMenu popup = new PopupMenu(HomeActivity.this, v);
popup.setOnMenuItemClickListener(HomeActivity.this);
popup.inflate(R.menu.popup_menu);
您可以使用 android:popupBackground
属性自定义 背景颜色。
<!-- Popup Menu -->
<style name="MyPopup" parent="@style/Widget.MaterialComponents.PopupMenu">
<item name="android:popupBackground">@color/custom</item>
</style>
您可以使用 popupMenuStyle
属性在您的应用主题中全局配置此样式:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="popupMenuStyle">@style/MyPopup</item>
</style>
就use:
PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu());
popup.show();
目前 PopupMenu 使用 colorOnPrimary
定义 textColor。要更改它,您必须定义一个 textAppearance:
<style name="MytextAppearanceLargePopupMenu" parent="@style/TextAppearance.MaterialComponents.Subtitle1">
<item name="android:textColor">@color/colorAccent</item>
</style>
然后在您的应用主题中添加此属性:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="textAppearanceLargePopupMenu">@style/MytextAppearanceLargePopupMenu</item>
</style>
将 popupMenu 样式添加到您的 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>
现在在您的清单文件中
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
.............
</application>
我是 android 的初学者。我想弹出菜单背景颜色 change.I 尝试使用此代码更改背景颜色,但它不适用于 me.i 不知道为什么不使用此代码。
<!--popup menu item style-->
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light">
<item name="android:textColor">@color/colorWhite</item>
<item name="android:popupBackground">@color/colorBlack</item>
<item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
<item name="android:divider">@color/colorWhite</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:popupBackground">@color/colorBlack</item>
<item name="overlapAnchor">false</item>
<item name="android:divider">@color/colorRegisterHeader</item>
<item name="android:dividerHeight">1dp</item>
</style>
//这个activity弹出菜单的代码
PopupMenu popup = new PopupMenu(HomeActivity.this, v);
popup.setOnMenuItemClickListener(HomeActivity.this);
popup.inflate(R.menu.popup_menu);
您可以使用 android:popupBackground
属性自定义 背景颜色。
<!-- Popup Menu -->
<style name="MyPopup" parent="@style/Widget.MaterialComponents.PopupMenu">
<item name="android:popupBackground">@color/custom</item>
</style>
您可以使用 popupMenuStyle
属性在您的应用主题中全局配置此样式:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="popupMenuStyle">@style/MyPopup</item>
</style>
就use:
PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu());
popup.show();
目前 PopupMenu 使用 colorOnPrimary
定义 textColor。要更改它,您必须定义一个 textAppearance:
<style name="MytextAppearanceLargePopupMenu" parent="@style/TextAppearance.MaterialComponents.Subtitle1">
<item name="android:textColor">@color/colorAccent</item>
</style>
然后在您的应用主题中添加此属性:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="textAppearanceLargePopupMenu">@style/MytextAppearanceLargePopupMenu</item>
</style>
将 popupMenu 样式添加到您的 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>
现在在您的清单文件中
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
.............
</application>