Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active December 21, 2015 12:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/6305009 to your computer and use it in GitHub Desktop.
Save daichan4649/6305009 to your computer and use it in GitHub Desktop.
物理メニューキーがある端末でも、ActionBar の overflowメニュー を強制的に表示する (for Android)
<application
android:name="daichan4649.actionbartest.TestApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="daichan4649.actionbartest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_test"
android:showAsAction="never"
android:title="@string/menu_test"/>
</menu>
public class TestApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
customOverflowMenu();
}
private void customOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
// 物理メニューキーの有無フラグ
// false を設定するとactionbarのoverflowメニューが必ず表示される
// true を設定するとactionbarのoverflowメニューは表示されない
// 物理メニューキーのない端末 かつ true 設定すると、
// メニュー出せなくなる。詰み。
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@daichan4649
Copy link
Author

物理メニューキーなし、を強制設定する(Applicationのカスタマイズ)
(ActionBar の overflowメニューは物理メニューキーの有無で変わる)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment