我想在我的Toolbar
(正在编写教程)中突出显示抽屉图标。为此,我需要它的位置。如何获得对抽屉导航图标(汉堡包)视图的引用?
问问题
9713 次
4 回答
29
您可以利用视图的内容描述,然后使用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;
}
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()
}
于 2015-04-29T22:57:23.877 回答
8
在调试模式下查看工具栏的子视图后,我看到抽屉图标可以在那里找到,作为 ImageButton。(感谢埃尔茨)
我使用带有 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。这个位置应该等于自定义工具栏布局中子视图的数量。
如果有人找到更好的解决方案,请告诉我。
于 2015-04-29T22:52:35.677 回答
8
如果您只想获取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;
}
于 2016-07-11T23:44:36.203 回答
0
即兴创作的@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<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;
}
于 2018-07-26T06:42:09.643 回答