How to get the “up” button used on the Toolbar?

醉酒当歌 提交于 2019-11-29 14:15:41

It seems this view doesn't have an ID

You're right, the navigation view is created programmatically and never sets an id. But you can still find it by using View.findViewsWithText.

View.findViewsWithText comes with two flags:

The navigation view's default content description is "Navigate up" or the resource id is abc_action_bar_up_description for AppCompat and action_bar_up_description for the framework's, but you can easily apply your own using Toolbar.setNavigationContentDescription.

Here's an example implementation:

final Toolbar toolbar = ...;
toolbar.setNavigationContentDescription("up");
setActionBar(toolbar);

final ArrayList<View> outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

outViews.get(0).setRotation(180f);

Results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!