Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是工具栏';s标志图标可点击?_Android_Android Actionbar_Toolbar_Android 5.0 Lollipop - Fatal编程技术网

Android 是工具栏';s标志图标可点击?

Android 是工具栏';s标志图标可点击?,android,android-actionbar,toolbar,android-5.0-lollipop,Android,Android Actionbar,Toolbar,Android 5.0 Lollipop,我已经使用了工具栏,所以现在我想在徽标图标上应用单击事件。我如何才能获得此事件 下面是我做的一些编码工作 Toolbar toolbar = null; toolbar = (Toolbar) findViewById(R.id.actionToolbar); setSupportActionBar(toolbar); setTitle(null); toolbar.setNavigationIcon(R.drawable.back); toolbar.setNavigationContentD

我已经使用了工具栏,所以现在我想在徽标图标上应用单击事件。我如何才能获得此事件

下面是我做的一些编码工作

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();
    }
});

在这里,我已经设置了导航图标和标志图标,所以现在我想标志图标的点击事件,它是如何可能的

您需要获得它的第一个参考

View logoView = getToolbarLogoView(toolbar);
logoView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //logo clicked
    }
});
使用内容描述,我们可以获得
视图
参考。请参阅内联注释

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;
    }
公共静态视图getToolbarLogoIcon(工具栏){ //检查之前是否设置了contentDescription 布尔hadContentDescription=android.text.TextUtils.isEmpty(toolbar.getlogosdescription()); String contentDescription=String.valueOf(!hadContentDescription?toolbar.getLogoDescription():“logoContentDescription”); toolbar.setLogoDescription(contentDescription); ArrayList potentialViews=新建ArrayList(); //根据视图的内容描述查找视图,通过编程或使用android:contentDescription进行设置 toolbar.findViewsWithText(潜在视图、内容描述、视图。FIND\u视图和内容描述); //导航图标始终在此时实例化,因为调用setLogoDescription可确保其存在 视图logoIcon=null; 如果(potentialViews.size()>0){ logoIcon=potentialViews.get(0); } //如果以前不存在,请清除内容说明 if(hadContentDescription) toolbar.setLogoDescription(空); 返回图标; }
我问自己同样的问题,遇到了这个问题。 我对Nikola Despotoski采取了类似的方法,但采用了不同的实现

我所做的不是方法,而是:

// Set drawable
toolbar.setLogo(ContextCompat.getDrawable(context, R.drawable.logo));

// Find logo
View view = toolbar.getChildAt(1);
view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      // Perform actions
    }
  });

有点黑客风格,但稍后会回来查看。分享以供讨论

appcompat:V7操作栏徽标视图(ImageView)的长点击事件行为


仅测试android 7.0

什么是getLogoContentDescription/setLogoContentDescription@MinaGabriel我已经编辑了我的答案。它应该是getLogoDescription和setLogoDescription。对不起,谢谢你。对我来说,childAt(0)是标志。child 1是标题这对我很有用,但我发现索引会改变,它可以是0或1。我不确定这两个活动之间有什么区别。区别在于徽标和标题的打开顺序。它有隐藏的元素,这些元素在启用时会添加到视图组中,这意味着第0个元素是第一个打开的元素。请编辑答案并添加代码意图或对想法的一点解释。
    ActionBar actionBar = getSupportActionBar();
    actionBar.setLogo(R.drawable.logo_vjet);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayUseLogoEnabled(true);
    //find ActionBar View(ToolBar)
    View view = getWindow().getDecorView().findViewById(android.support.v7.appcompat.R.id.action_bar);
    if(view != null && view instanceof Toolbar){
        try {
            //find ImageView mLogoView; in Toolbar using reflect
            Field logoView = view.getClass().getDeclaredField("mLogoView");
            logoView.setAccessible(true);
            ImageView logoImageVIew = (ImageView) logoView.get(view);
            if(logoImageVIew != null){
                logoImageVIew.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        //do something
                        return false;
                    }
                });
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }