带有主页和后退按钮的 Android 工具栏 [英] Android Toolbar with both home and back button

查看:24
本文介绍了带有主页和后退按钮的 Android 工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在工具栏中同时显示主页图标和返回图标?1)是否可以更改后退按钮图标和主页图标的显示顺序.目前它先显示箭头按钮,然后显示徽标(主页按钮)

Is it possible to display both home icon and back icon in the toolbar? 1) Is it possible change the order of display of back button icon and home icon. Currently it displays the arrow button first and then the logo (home button)

2) 第二个要求是在单击主页图标时清除活动堆栈,并在返回按钮的情况下返回上一屏幕.

2) Second requirement is to clear the activity stack on clicking the home icon and going back to previous screen in case of back button.

我有以下代码,它将显示一个箭头返回键和设置为徽标的主页图标.是否可以处理这两个图标上的点击事件:

I have the following code which will display a arrow back key and home icon which is set as logo. Is it possible to handle click events on both these icons:

Toolbar toolbar = (Toolbar)findByViewID(R.id.toolbar);
toolbar.setNavigationIcon(R.drwable.btn_back);
setSuppportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.home_icon);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

我可以通过在 onOptionsITemSelected 方法中处理单击箭头图标来处理它.有没有办法处理点击徽标图标?我的想法是使用主页按钮单击清除活动堆栈并使用后退按钮导航回上一个屏幕.

I am able to handle to the click on arrow icon by handling it in onOptionsITemSelected method. Is there a way to handle click on logo icon? My idea is to use the home button click to clear the stack of activities and use the back button to navigate back to previous screen.

我试过了

toolbar.setNavigationOnClickListener() 

但它对点击后退按钮没有影响.

but it has no effect on back button click.

onOptionsItemSelected()

推荐答案

用于导航返回.这对我有用.

For navigating back. This worked for me.

@Override 
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                Intent homeIntent = new Intent(this, HomeActivity.class);
                homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
        } 
        return (super.onOptionsItemSelected(menuItem));
    } 

这篇关于带有主页和后退按钮的 Android 工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆