gpt4 book ai didi

android - 工具栏的 Logo 图标是否可点击?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:18:40 26 4
gpt4 key购买 nike

我已经使用了工具栏,所以现在我想在 Logo 图标上应用点击事件,我怎样才能获得此事件?

这是我写的一些代码

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

我在这里设置了导航图标和 Logo 图标,所以现在我想要 Logo 图标的点击事件,这怎么可能?

最佳答案

你需要得到它的第一个引用

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

使用内容描述我们可以获得View引用。请参阅内联评论。

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

关于android - 工具栏的 Logo 图标是否可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28136087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com