android - 获取 Toolbar 的导航图标 View 引用

标签 android android-actionbar navigation-drawer toolbar hamburger-menu

我想在我的 Toolbar 中突出显示抽屉图标(正在编写教程)。为此,我需要它的位置。如何获得对抽屉导航图标(汉堡) View 的引用?

最佳答案

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

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

关于android - 获取 Toolbar 的导航图标 View 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29922995/

相关文章:

android - Webview 中的 HTML 内容重叠

android - 在 Android 中自定义 ActionBar TabBar?

android - 在不继承 ActionBarActivity 的情况下添加操作栏

android - 菜单按钮未显示在操作栏上

android - 为什么 Activity 的方法 onCreateOptionMenu() 通过使用 setHasOptionMenu(true) 来调用它的 Fragments 两次?

android - 扩展时更改 ModalDrawer 的大小,Jetpack Compose

android - 未收到来自 Chrome 自定义选项卡菜单项的广播点击

java - Ui 击中 Firebase Firestore 删除数据?

java - 单击抽屉导航项目时,新 Activity 不会启动

android - 如何在 OnCompleteListener Firebase 中使用 Async/Await/Coroutines