When targeting API 26, overflow menu items become invisible

我是研究僧i 提交于 2021-02-08 02:36:41

问题


I have an options menu in my app, which has more than six items, so there is a "More" item that brings up the overflow menu. The menu is posted by the default menu triggers, and a custom trigger that invokes Activity.openOptionsMenu().

The menu itself is built from this resource file:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/mi_copy"
          android:title="Copy"
          android:icon="@drawable/copy"/>
    <item android:id="@+id/mi_paste"
          android:title="Paste"
          android:icon="@drawable/paste"/>
    <item android:id="@+id/mi_preferences"
          android:title="Preferences"
          android:icon="@android:drawable/ic_menu_preferences"/>
    <item android:id="@+id/mi_flip_calc_printout"
          android:title="Print-Out"
          android:icon="@drawable/printer"/>
    <item android:id="@+id/mi_clear_printout"
          android:title="Clear Print-Out"
          android:icon="@android:drawable/ic_menu_close_clear_cancel"/>
    <item android:id="@+id/mi_about"
          android:title="About Free42"/>
    <item android:id="@+id/mi_import"
          android:title="Import Programs"/>
    <item android:id="@+id/mi_export"
          android:title="Export Programs"/>
</menu>

So far, so good. The menu and the Java logic that creates and posts it has worked for years, but now I'm changing the targetSdkVersion from API level 8 to 26, per the latest Play requirements, and now all of a sudden, the menu items in the overflow menu have become invisible.

The overflow menu still posts, and it still has the correct size, and the menu items still work. You just can't see them any more: the whole menu is solid black now, which is the normal background color; the menu item text is missing.

The phone on which I'm testing is a Motorola G5 running Android 8.1.0. This is the only device I have available for testing with API 26 at the moment, at least until I figure out how to create an AVD with API >= 26.

N.B. In the Android simulator with an API level 8 image, the overflow menu still looks fine. It used to look fine on the Moto G5 as well, before I targeted API 26. If I change the target back to 8, the overflow menu works again.

Any thoughts on what I could do to fix this? My Google skills aren't working on this one... Any ideas would be most welcome!


回答1:


I added this style file as res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Free42Theme" parent="android:style/Theme.NoTitleBar">
        <item name="android:itemBackground">@android:color/white</item>
    </style>

    <style name="Free42Theme.Fullscreen" parent="android:style/Theme.NoTitleBar.Fullscreen">
        <item name="android:itemBackground">@android:color/white</item>
    </style>

</resources>

The first is the default theme, specified by changing the android:theme attribute on the Activity from @android:style/Theme.NoTitleBar to @style/Free42Theme in AndroidManifest.xml; the second is the full-screen theme, set in the Java code, by changing the setTheme(android.R.style.Theme_NoTitleBar_Fullscreen) call to setTheme(R.style.Free42Theme_Fullscreen).

This changes the background of the overflow menu to white, making the black item text visible, verified on my G5. Running in the simulator with Android 2.2 (API level 8), the appearance is unchanged since the background was white there from the start.

Many thanks to SteelToe for getting me on the right track!



来源:https://stackoverflow.com/questions/54165132/when-targeting-api-26-overflow-menu-items-become-invisible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!