获取工具栏的导航图标视图参考
Get Toolbar's navigation icon view reference
我想在我的Toolbar
(正在编写教程)中突出显示抽屉图标。为此,我需要它的位置。如何获取对抽屉导航图标(汉堡包)视图的引用?
在调试模式下查看工具栏的子视图后,我看到抽屉图标可以在那里找到,作为一个 ImageButton。 (感谢 Elltz)
我使用带有自定义 xml 布局的工具栏和 2 个子项(LinearLayout 和 ImageView),所以我的工具栏最终有 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。这个位置应该等于自定义工具栏布局中子视图的数量。
如果有人找到更好的解决方案,请告诉我。
您可以利用视图的内容描述,然后使用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()
}
如果你只想获取代表工具栏导航图标的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;
}
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;
}
我想在我的Toolbar
(正在编写教程)中突出显示抽屉图标。为此,我需要它的位置。如何获取对抽屉导航图标(汉堡包)视图的引用?
在调试模式下查看工具栏的子视图后,我看到抽屉图标可以在那里找到,作为一个 ImageButton。 (感谢 Elltz)
我使用带有自定义 xml 布局的工具栏和 2 个子项(LinearLayout 和 ImageView),所以我的工具栏最终有 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。这个位置应该等于自定义工具栏布局中子视图的数量。
如果有人找到更好的解决方案,请告诉我。
您可以利用视图的内容描述,然后使用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()
}
如果你只想获取代表工具栏导航图标的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;
}
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;
}