Android: menu in action bar

2019-07-26 17:57发布

i have problem putting the menu there, in the upper right corner:

android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.mio.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11"
    android:targetSdkVersion="15" />
<application android:allowBackup="false"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Light">
    <activity android:name=".Lista_preventivi"
        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>

Java:

public class Lista_preventivi extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista_preventivi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}
}

menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
    android:showAsAction="ifRoom"
    android:title="@string/action_settings" />
</menu>

all works, but the menu appear only if i click the physical button on my phone, and it appear at the bottom like a normal menu.

ho can i have the icon menu in the upper right action bar, and show the menu there?

Thanks!

2条回答
\"骚年 ilove
2楼-- · 2019-07-26 18:31

The theme which your are applying to the application is android:theme="@android:style/Theme.Light". it doesn't have an actionbar.

Change it to something which has an action bar like,

  • Theme.Sherlock.Light (Need to add ABS library)
  • Theme.Holo.Light

The code is just fine.

查看更多
Fickle 薄情
3楼-- · 2019-07-26 18:32

Add: android:showAsAction="always" in your manifest file If your sdk version is >3.0 .Else you have no other option other than implementing Sherlock Home Api.

查看更多
登录 后发表回答