有人可以帮助我在操作栏中创建一个向上按钮

时间:2012-06-25 02:33:32

标签: android

开发一个应用程序,其中主视图将使用仪表板设计。我在android设计模式中看到他们引入了使用Up Button的新概念。我混淆了一点因为按钮和后退按钮总是相同的,除了你可以使用向上按钮直接进入你的家庭视图。有人能告诉我如何在动作栏sherlock中创建一个向上按钮吗?请帮忙。谢谢。

1 个答案:

答案 0 :(得分:11)

如果您已经实施了操作栏,则应该可以使用以下代码设置主页按钮。

这是onCreate

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);

这是一个单独的方法

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intentHome = new Intent(this, TargetActivity.class);
            intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intentHome);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

如果您尝试做同样的事情,这应该有用。