简体   繁体   中英

Android: menu in action bar

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!

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.

在清单文件中添加: android:showAsAction="always"如果sdk版本> 3.0。否则,除了实现Sherlock Home Api之外,您没有其他选择。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM