我想突出显示Toolbar
中的抽屉图标(处理教程)。为此,我需要它的立场。如何获得抽屉式导航图标(汉堡包)视图的参考?
答案 0 :(得分: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;
}
答案 1 :(得分: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;
}