主页右侧的 ActionBar 按钮

ActionBar button to right of Home

我需要在操作栏上后退箭头的右侧放置一个按钮(主页)。要求是左箭头(通常是主页按钮)被捕获并执行我正在工作的 onBackPressed 。现在我如何在左箭头的右侧添加一个按钮,以便当我点击它时我可以在 onOptionsItemSelected 上捕获它并让它执行 Home 功能。我尝试创建一个带有单个图像视图的相对布局作为测试,然后在用 actionBar.setCustomView(v); 设置视图后设置它;然而它确实出现了。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_horizontal">

        <ImageButton
            android:id="@+id/bookmark_home_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/common_google_signin_btn_icon_dark" />


    </RelativeLayout>

所以最终创建了一个自定义操作栏,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/bookmark_actionbar_left_btn"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@null"
        android:src="@drawable/navigation_back" />

    <ImageButton
        android:id="@+id/bookmark_actionbar_logo_btn"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@null"
        android:paddingLeft="20dp"
        android:src="@drawable/app_launcher" />

</LinearLayout>

然后在 onCreate

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.bookmark_actionbar);

ImageButton goBackBtn = (ImageButton) findViewById(R.id.bookmark_actionbar_left_btn);
    goBackBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

ImageButton goHomeBtn = (ImageButton) findViewById(R.id.bookmark_actionbar_subrosa_btn);
    goHomeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NavUtils.navigateUpFromSameTask(BookmarkList.this);
        }
    });

所以我最终使用了自定义 ActionBar - 请参阅上面的更新