如何在Android的操作栏中添加ImageButton?

use*_*145 2 android android-actionbar

我要自定义我的操作栏。我想在操作栏中添加一个ImageButton,单击该按钮将转到另一个活动。我的代码如下。我不知道继续前进。谁能建议我一步一步去做。

    final ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.blue));
Run Code Online (Sandbox Code Playgroud)

Sam*_*sso 5

要将按钮添加到操作栏,请在活动中找到要向其添加按钮的操作栏的onCreateOptionsMenu方法。接下来转到res> menu> main并根据此示例添加项目:

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

    <item
        android:id="@+id/action_settings" //<-- id of the view
        android:orderInCategory="100" //<-- not important for the moment (importance)
        android:showAsAction="never" //<-- this will always put it in overflow. set always to show as action
        android:icon="@drawable/ic_launcher"/> //<-- Icon to display in action bar
        android:title="@string/action_settings"/> //<-- not really important

</menu>
Run Code Online (Sandbox Code Playgroud)

接下来,我们将要侦听项目的点击,因此将此方法添加到您的活动中:

    @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // action with ID action_settings was selected
        case R.id.action_settings:
        // this is where you put your own code to do what you want.
break;
        default:
          break;
        }

        return true;
      } 
Run Code Online (Sandbox Code Playgroud)

你们去!