【发布时间】:2025-12-15 15:40:01
【问题描述】:
有没有人能够做到这一点。 UiScrollable、swipeLeft 和 swipeRight 似乎对它没有任何影响。我正在使用带有最新 api 的 Nexus 5 模拟器。有人能把它拉下来吗?
【问题讨论】:
-
请看一下编辑后的答案。希望对您有所帮助。
标签: android navigation-drawer android-uiautomator
有没有人能够做到这一点。 UiScrollable、swipeLeft 和 swipeRight 似乎对它没有任何影响。我正在使用带有最新 api 的 Nexus 5 模拟器。有人能把它拉下来吗?
【问题讨论】:
标签: android navigation-drawer android-uiautomator
ActionBarDrawerToggle constructor 中设置的内容描述
ActionBarDrawerToggle,其中包含打开和关闭的内容说明。
// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";
// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";
private void setUpNavigationDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, CONTENT_DESCRIPTION_OPEN_DRAWER, CONTENT_DESCRIPTION_CLOSE_DRAWER);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
UiAutomatorTestCase 中,要打开/关闭抽屉,请通过步骤 1 中定义的打开/关闭内容描述找到 UI 对象并单击它。
// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";
// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";
private void openNavDrawer() throws UiObjectNotFoundException {
findViewByContentDescription(CONTENT_DESCRIPTION_OPEN_DRAWER).click();
}
private void closeNavDrawer() throws UiObjectNotFoundException {
findViewByContentDescription(CONTENT_DESCRIPTION_CLOSE_DRAWER).click();
}
private UiObject findViewByContentDescription(String description) {
return new UiObject(new UiSelector().description(description));
}
警告:如果您使用Material design approach 作为导航抽屉(抽屉在Topbar 顶部和状态栏后面打开),将绘制“汉堡”图标在抽屉后面,使closeDrawer() 方法不起作用。作为一种解决方法,您只需选择抽屉菜单中打开的部分即可;这会关闭抽屉并显示与打开之前相同的部分。
【讨论】:
你可以试试这个:
UiObject view = new UiObject(new UiSelector()."use what you want to identify the view");
Rect bounds = view.getBounds(); // Returns the bounds of the view as (left, top, right, bottom)
int center_x = bounds.centerX();
int center_y = bounds.centerY();
// Now you have the center of the view. You can just slide by using drag()
getUiDevice().drag(center_x, center_y, center_x + "amount of pixels", center_y)
// Where amount of pixels can be a variable set to a fraction of the screen width
我在文件浏览器应用程序上从左到右滑动的抽屉上使用了这个想法。 我没有使用与上面完全相同的代码,因为我正在为 uiautomator 使用 python 包装器。 (https://github.com/xiaocong/uiautomator)
【讨论】: