AppCompatActivity 操作栏和设备菜单按钮

AppCompatActivity actionbar and device menu button

我正在使用具有 ActionBar 样式的 AppCompatActivity @style/Widget.AppCompat.Light.ActionBar。当我从 activity 创建菜单时,如果设备没有硬件菜单按钮,则溢出菜单按钮会出现在 ActionBar 中。 但是,在小米 Redmi Note 设备上,溢出按钮出现在 ActionBar 中,并且设备有自己的硬件按钮。 这是一些配置:

compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    minSdkVersion 10
    targetSdkVersion 22
}

appcompat-v7 支持库强制溢出菜单在每个装有 KitKat 及更高版本的设备的操作栏(或工具栏)中始终可见。 MENU 键(如果存在)仍然可以打开溢出菜单。这是一种理想的行为,它被设计成这样。

代码非常简单:

public boolean showsOverflowMenuButton() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return true;
    } else {
        return !ViewConfigurationCompat.hasPermanentMenuKey(ViewConfiguration.get(mContext));
    }
}

您可以在此处查看完整的源代码: https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/src/android/support/v7/internal/view/ActionBarPolicy.java#L49-L55