简体   繁体   中英

ActionBar buttons are not displayed

I'm trying to display my menu in the action bar like this :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/Menu1"
        android:icon="@drawable/res1"
        android:showAsAction="ifRoom"
        android:title="Menu1"/>
    <item
        android:id="@+id/Menu2"
        android:icon="@drawable/res2"
        android:showAsAction="ifRoom"
        android:title="Menu2"/>
    <item
        android:id="@+id/Menu3"
        android:icon="@drawable/res3"
        android:showAsAction="ifRoom"
        android:title="Menu3"/>

</menu>

And in the code :

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

On the design editor the buttons are shown. But when I launch the app on my smartphone (Samsung Galaxy Note 3) the 3 dots are shown with the menu inside it. But no menu in the Action Bar.

I don't understand, I tested all flags for android:showAsAction and I still have the 3 dots with the menu inside.

Thanks for your help !

import this

app xmlns:app="http://schemas.android.com/apk/res-auto"

and use this app:showAsAction="always" in place of android:showAsAction="ifRoom"

Edited full Code

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
app xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/Menu1"
        android:icon="@drawable/res1"
        app:showAsAction="always"
        android:title="Menu1"/>
    <item
        android:id="@+id/Menu2"
        android:icon="@drawable/res2"
        app:showAsAction="always"
        android:title="Menu2"/>
    <item
        android:id="@+id/Menu3"
        android:icon="@drawable/res3"
        app:showAsAction="always"
        android:title="Menu3"/>

</menu>

try using this

<menu 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
 <item
android:id="@+id/Menu1"
android:icon="@drawable/ic_launcher"
app:showAsAction="always"
android:title="Menu1"
android:orderInCategory="0"/>

<item
android:id="@+id/Menu2"
android:icon="@drawable/ic_launcher"
app:showAsAction="always"
android:title="Menu2"
android:orderInCategory="1"/>

<item
android:id="@+id/Menu3"
android:icon="@drawable/ic_launcher"
app:showAsAction="always"
android:title="Menu3"
android:orderInCategory="2"/>

</menu>

try below menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/Menu1"
    android:icon="@drawable/ic_launcher"
    app:showAsAction="ifRoom"
    android:title="Menu1"
    android:orderInCategory="0"/>
<item
    android:id="@+id/Menu2"
    android:icon="@drawable/ic_launcher"
    app:showAsAction="ifRoom"
    android:title="Menu2"
    android:orderInCategory="1"/>
<item
    android:id="@+id/Menu3"
    android:icon="@drawable/ic_launcher"
    app:showAsAction="ifRoom"
    android:title="Menu3"
    android:orderInCategory="2"/>

</menu>

Try this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item
        android:id="@+id/Menu1"
        android:icon="@drawable/res1"
        android:title="Menu1"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/Menu2"
        android:icon="@drawable/res2"
        android:title="Menu2"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/Menu3"
        android:icon="@drawable/res3"
        android:title="Menu3"
        app:showAsAction="ifRoom" />

</menu>

Also, take a look:

http://developer.android.com/guide/topics/resources/menu-resource.html

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@[+][package:]id/resource_name"
          android:title="string"
          android:titleCondensed="string"
          android:icon="@[package:]drawable/drawable_resource_name"
          android:onClick="method name"
          android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

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