如何用imagebutton替换设置项目

问题描述:

我正在开发android工作室的导航抽屉活动,在菜单文件夹左侧有一个设置项。我想删除它并在操作栏上添加一个图像按钮。任何人都可以告诉我如何添加该图像按钮以及如何在主要活动中的该按钮上执行点击事件。如何用imagebutton替换设置项目

检查下面的图片以供参考 -

enter image description here

我希望这可以帮助,加上这是你menu.xml布局内 -

的showAsAction属性允许您定义显示操作。例如,ifRoom属性定义如果有足够的可用屏幕空间,操作仅显示在操作栏中。

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

    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:showAsAction="always" 
     android:icon="@drawable/ic_settings" 
     android:title="Settings"> 
    </item> 

</menu> 

导航在菜单文件夹main_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/image_button" 
     android:icon="@android:drawable/ic_menu_search" 
     android:title="Clickme" 
     app:showAsAction="always" /> 

</menu> 

在您的MainActivity

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

@Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     if(item.getItemId == R.id.image_button){ 
     //do action on imagebutton click 
     return true; 
     } 
    return super.onOptionsItemSelected(item); 
    } 

只是试试这个。
无需添加的ImageButton你只需要添加

android:showAsAction="always" 

和图标,

android:icon="@drawable/Your_icon" 

在menu.xml文件文件为此,在项目设置