Android - actionBar.setDisplayHomeAsUpEnabled 问题

标签 android android-actionbar actionbarsherlock android-fragmentactivity

在我扩展 SherlockFragmentActivity 的 Activity 中,我有操作栏并且还设置了 actionBar.setDisplayHomeAsUpEnabled(true) 以返回到我之前的 Activity 。一切正常。但是当我点击主页按钮时,背景颜色应该只应用于应用程序图标,但它也适用于操作栏上的“标题”,如屏幕截图所示。我不希望这发生。当我点击主页按钮时,只有应用程序图标应该可以点击(背景颜色应该只应用于应用程序图标)而不是标题。知道我该怎么做吗?

enter image description here

我的 Activity 代码:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(some_string);

最佳答案

不要忘记这部分,因为主页按钮也是一个菜单按钮。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // This is called when the Home (Up) button is pressed in the action bar.
            // Create a simple intent that starts the hierarchical parent activity and
            // use NavUtils in the Support Package to ensure proper handling of Up.
            Intent upIntent = new Intent(this, MainActivity.class);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // This activity is not part of the application's task, so create a new task
                // with a synthesized back stack.
                TaskStackBuilder.from(this)
                        // If there are ancestor activities, they should be added here.
                        .addNextIntent(upIntent)
                        .startActivities();
                finish();
            } else {
                // This activity is part of the application's task, so simply
                // navigate up to the hierarchical parent activity.
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}

关于Android - actionBar.setDisplayHomeAsUpEnabled 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15173199/

相关文章:

android - 强制使用 ActionBarSherlock 的应用程序使用 Theme 而不是 Theme.Holo

java - Android - 显示/隐藏 ActionBar 项目时淡入/淡出

带有搜索 View 的 Android 操作栏

android - 访问本地主机 :port from Android emulator

android - 折叠的工具栏标题与我的菜单图标重叠

android - 将 View 放置在带有叠加层的操作栏下方

android - 如何从 fragment 中获取工具栏?

安卓 repo 初始化 : SSL_CERTIFICATE_VERIFY_FAILED error

java - 解析 JSON 数组

java - 安卓。立即隐藏状态栏