简体   繁体   中英

How to add Custom ActionBar item?

<item
    android:id="@+id/notification_button"
    android:actionLayout="@layout/notification_count"
    android:title="Badges"
    android:showAsAction="always">

The ActionLayout is not working but title is showing, how can I fix this ?

MainActivity

@Override
        public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       switch (item.getItemId()) 
       {
         case R.id.search:  // it is going to refer the search id name in main.xml

         //add your code here

            return true;
         default:
            return super.onOptionsItemSelected(item);
       }
    }

main.xml

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

    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_action_settings"  --> add a icon in drawable that will display in the top of the action bar.
        android:title="@string/search"
        android: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