如何获得Android的菜单自定义按钮在操作栏中以同样的方式自定义、栏中、如何获得、按钮

2023-09-12 03:20:12 作者:笑我妄想

由于设计需要,我不能使用操作栏。 随着旧的菜单按钮,这并不重要,因为所有的菜单命令都显示在一个特定的菜单,不幸的是一些设备如的Nexus 4还没有一个菜单按钮,用户就不能访问的功能菜单。

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.

所以我想包括我个人的按钮就像在操作栏(​​或物理菜单按钮)右上角边缘的按钮,将自动获得在OptionsMenu的所有命令,但我不知道我怎么可以这样做这一点没有单独重新定义一个按钮,每一个命令菜单和我的自定义按钮,菜单。

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.

有人可以帮助我吗?

推荐答案

所以,首先我知道,你应该用你的设计的东西去,但它的确是一个更好的选择,使用动作条 ActionBarSherlock 新的应用程序。但为了得到正确的答案你的问题,你应该显示在旁边的家庭和后退按钮底部的系统栏的软件菜单按钮之前做了几件事情。

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;
}

和你应该更改第二件事就是在你的的Andr​​oidManifest.xml

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>

看看这行:

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

这就是奇迹发生的地方。如果您设置 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.

如果你所做的一切如上,你会得到Nexus4 / Nexus7的屏幕是这样的:

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

希望这有助于!

P.S 。而刚刚再说,构建应用程序,你应该考虑使用编码的最新的设计指南和最佳实践。

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