如何添加Custom ActionBar项目?

时间:2016-05-09 13:11:42

标签: java android-actionbar

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

ActionLayout无效,但标题显示,我该如何解决?

1 个答案:

答案 0 :(得分:0)

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>