简体   繁体   中英

How to show menu item in action bar?

I want the menu items to show on action bar. There is a lot of space on the ActionBar, but still they don't show up. Every item shown as three dots, why?

.java file

public class GalleryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_layout);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
}

.xml file

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

        <item
            android:id="@+id/facebookId"
            android:title="facebook"
            android:icon="@drawable/facebook"
            MissionAndroid:showAsAction="always"/>

        <item
            android:id="@+id/shareId"
            android:title="share"
            android:icon="@drawable/share"
            MissionAndroid:showAsAction="always"/>

        <item
            android:id="@+id/delete"
            android:title="delete"
            android:icon="@drawable/delete"
            MissionAndroid:showAsAction="always"/>

        <item
            android:id="@+id/searchId"
            android:title="search"
            android:icon="@drawable/search"
            MissionAndroid:showAsAction="ifRoom"/>
    </menu>

Currently, you're using "http://schemas.android.com/tools" which does not offer the functionality you're looking for and your showAsAction modifiers are being ignored.

Try updating your main_menu.xml with the following:

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

    ...

</menu>

Better yet, to follow convention, replace MissionAndroid in this file with app :

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

    <item
        android:id="@+id/facebookId"
        android:title="facebook"
        android:icon="@drawable/facebook"
        app:showAsAction="always"/>

    ...

</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