简体   繁体   中英

Android actions not showing up in actionbar

I have been trying to program a simple app for taking notes on android. I want to have an "add" button showing in the actionbar of the main activity. However, it only shows up as an overflow. I tried

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_add"
    android:icon="@drawable/ic_add"
    android:title="@string/add"
    android:showAsAction="always"></item>
</menu>

but I cannot run the app because android studio tells me

should use app:showAsAction with the appcompat library with xmlns:app="http://schemas.android.com/apk/res-auto"

Any ideas? I am not, to my knowledge, using appcompat

Edit: the java class:

public class NoteChoice extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notechooser);
    SQLiteDatabase db = openOrCreateDatabase("Notes", MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, val VARCHAR )");


}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.notechooser_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case R.id.action_add:
            onPause();
            startActivity(new Intent("com.example.theorangeutan.notes.Create"));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

The styles.xml file:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <!-- Customize your theme here. -->
</style>
<style name="splash" parent="@android:style/Theme.Holo.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

support-v7 is considered a 3rd party library, so XML attributes introduced via support-v7 must use the app namespace instead of android namespace so that backwards compatibility to v7 is guaranteed. For example, android:showAsAction is only available >= SDK 11, while app:showAsAction is available >= SDK 7.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_add"
    android:icon="@drawable/ic_add"
    android:title="@string/add"
    app:showAsAction="always"></item>
</menu>

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