获取工具栏的导航图标视图参考

我想在Toolbar突出显示抽屉图标(处理教程)。 为此,我需要它的立场。 如何获得抽屉导航图标(汉堡包)视图的参考?

    您可以使用视图的内容描述,然后使用findViewWithText()方法获取视图引用

      public static View getToolbarNavigationIcon(Toolbar toolbar){ //check if contentDescription previously was set boolean hadContentDescription = TextUtils.isEmpty(toolbar.getNavigationContentDescription()); String contentDescription = !hadContentDescription ? toolbar.getNavigationContentDescription() : "navigationIcon"; toolbar.setNavigationContentDescription(contentDescription); ArrayList potentialViews = new ArrayList(); //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 setNavigationContentDescription ensures its existence View navIcon = null; if(potentialViews.size() > 0){ navIcon = potentialViews.get(0); //navigation icon is ImageButton } //Clear content description if not previously present if(hadContentDescription) toolbar.setNavigationContentDescription(null); return navIcon; } 

    更多

    在调试模式下查看工具栏的子视图后,我看到可以在那里find抽屉图标,作为ImageButton。 (感谢Elltz)

    我使用带有2个子项(LinearLayout和ImageView)的自定义xml布局的工具栏,所以我的工具栏最后有4个孩子,这些位置:

     [0] LinearLayout(from custom xml) [1] ImageView(from custom xml) [2] ImageButton(drawer icon) [3] ActionMenuView(menu icon) 

    知道了这一点,我现在可以使用:

     View drawerIcon = toolbar.getChildAt(2); 

    获取对抽屉菜单图标的引用。 在我的例子中,位置是2.此位置应该等于自定义工具栏布局中的子视图的数量。

    如果有人find更好的解决方案,请告诉我。

    如果您只想让Drawable代表工具栏导航图标,您可以这样做:

     Drawable d = mToolbar.getNavigationIcon(); 

    您可以通过如下方法获得对用于工具栏导航图标的ImageButton的引用:

     public ImageButton getToolbarNavigationButton() { int size = mToolbar.getChildCount(); for (int i = 0; i < size; i++) { View child = mToolbar.getChildAt(i); if (child instanceof ImageButton) { ImageButton btn = (ImageButton) child; if (btn.getDrawable() == mToolbar.getNavigationIcon()) { return btn; } } } return null; } 

    即兴@Nikola Despotoski的答案

     public static View getNavigationIconView(Toolbar toolbar) { String previousContentDescription = (String) toolbar.getNavigationContentDescription(); // Check if contentDescription previously was set boolean hadContentDescription = !TextUtils.isEmpty(previousContentDescription); String contentDescription = hadContentDescription ? previousContentDescription : "navigationIcon"; toolbar.setNavigationContentDescription(contentDescription); ArrayList potentialViews = new ArrayList<>(); // Find the view based on it's content description, set programmatically or with // android:contentDescription toolbar.findViewsWithText(potentialViews, contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION); // Nav icon is always instantiated at this point because calling // setNavigationContentDescription ensures its existence View navIcon = null; if (potentialViews.size() > 0) { navIcon = potentialViews.get(0); //navigation icon is ImageButton } // Clear content description if not previously present if (!hadContentDescription) toolbar.setNavigationContentDescription(previousContentDescription); return navIcon; }