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

问题描述 投票:13回答:4

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

android android-actionbar navigation-drawer toolbar hamburger-menu
4个回答
21
投票

您可以使用视图的内容描述,然后使用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<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 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;
     }

More

Kotlin扩展属性:

val Toolbar.navigationIconView: View?
        get() {
            //check if contentDescription previously was set
            val hadContentDescription = !TextUtils.isEmpty(navigationContentDescription)
            val contentDescription = if (hadContentDescription) navigationContentDescription else "navigationIcon"
            navigationContentDescription = contentDescription
            val potentialViews = arrayListOf<View>()
            //find the view based on it's content description, set programatically or with android:contentDescription
            findViewsWithText(potentialViews, contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION)
            //Clear content description if not previously present
            if (!hadContentDescription) {
                navigationContentDescription = null
            }
            //Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existence
            return potentialViews.firstOrNull()
        }

7
投票

在调试模式下查看工具栏的子视图后,我看到可以在那里找到抽屉图标,作为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.此位置应该等于自定义工具栏布局中的子视图的数量。

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


5
投票

如果您只想获得代表工具栏导航图标的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;
}

0
投票

即兴创作的@Nikola Despotoski's答案

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<View> 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.