How to get android menu in a custom button in the same way of action bar

倾然丶 夕夏残阳落幕 提交于 2019-12-11 04:23:29

问题


Due to design needs I cannot use the Action Bar. With the old menu button this is not important since all menu commands are displayed in a specific menu, unfortunately some devices as Nexus 4 haven't a menu button and the user cannot access the function of menu.

So I want to include a my personal button that works like the button in the top-right edge of action bar (or physical menu button), getting all commands in OptionsMenu automatically, but I don't know how could I do this without redefine a button for each command separately for menu and for my custom button-menu.

Someone could help me?


回答1:


So, first of all I know that you should go with your design stuff, but it's really a better option to use ActionBar or ActionBarSherlock for new applications. But in order to give the right answer to your question, you should do a few things before showing software menu button in bottom system bar next to home and back button.

First of all include your menu as always :

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

And second thing which you should change is in your AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="10" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.test.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>

</manifest>

Take a look at this line :

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="10" />

this is the place where magic happens. If you set your targetSdkVersion bigger than 10, it means that your app supports devices with no hardware menu button and probably use ActionBar or some other way.

If you do everything as above you will get on Nexus4/Nexus7 screen like this :

Hope this helps!

P.S. And just to say again, building application you should consider using the latest available design guidelines and best practices in coding.




回答2:


If I understand you correctly, you want a Button that opens up your menu.xml or whatever you may be calling it. If so, you can use PopupMenu. Note that this requires API >= 11. I had to create my own "ActionBar" to get the look and functionality that I needed and PopupMenu has worked out nicely. This allows you to inflate R.menu.your_menu and has interfaces and methods that allow you to use it as a menu.

I put my functionality into a BaseActivity which is a super class for all of my other Activities then I can override onMenuItemClick if I need different functionality in certain Activities.

Let me know if I misunderstand what you need.



来源:https://stackoverflow.com/questions/17432603/how-to-get-android-menu-in-a-custom-button-in-the-same-way-of-action-bar

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