工具栏的徽标图标可以单击吗? [英] Is toolbar's logo icon clickable?

查看:57
本文介绍了工具栏的徽标图标可以单击吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用过工具栏,所以现在我想将单击事件应用到徽标图标上,如何获得此事件?

i have used toolbar so now i want to apply click event over logo icon how can i get this event?

这是我做过的一些编码工作

here is some coding stuff that i have done

Toolbar toolbar = null;
toolbar = (Toolbar) findViewById(R.id.actionToolbar);
setSupportActionBar(toolbar);
setTitle(null);
toolbar.setNavigationIcon(R.drawable.back);
toolbar.setNavigationContentDescription("BACK");
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setLogoDescription("LOGO");

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Nav", Toast.LENGTH_SHORT).show();
    }
});

在这里,我已经设置了导航图标和徽标图标,所以现在我想要徽标图标的click事件,这怎么可能?

Here i have set navigation icon and logo icon so now i want logo icon's click event, how it is possible?

推荐答案

您需要先获取它的参考

View logoView = getToolbarLogoView(toolbar);
logoView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //logo clicked
    }
});

使用内容描述,我们可以获得View参考.查看内联评论.

Using content description we can get View reference. See the comments inline.

public static View getToolbarLogoIcon(Toolbar toolbar){
        //check if contentDescription previously was set
        boolean hadContentDescription = android.text.TextUtils.isEmpty(toolbar.getLogoDescription());
        String contentDescription = String.valueOf(!hadContentDescription ? toolbar.getLogoDescription() : "logoContentDescription");
        toolbar.setLogoDescription(contentDescription);
        ArrayList<View> potentialViews = new ArrayList<View>();
        //find the view based on it's content description, set programatically or with android:contentDescription
        toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        //Nav icon is always instantiated at this point because calling setLogoDescription ensures its existence 
        View logoIcon = null;
        if(potentialViews.size() > 0){
            logoIcon = potentialViews.get(0);
        }
        //Clear content description if not previously present
        if(hadContentDescription)
            toolbar.setLogoDescription(null);
        return logoIcon;
    }

这篇关于工具栏的徽标图标可以单击吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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