Getting screen coordinate of action bar menu item for creating introduction screen

我们两清 提交于 2019-12-03 13:55:35
penduDev

Here's how I did it:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId().equals(R.id.my_menu_item)) {
        View menuView = findViewById(R.id.menu_item_search);
        int[] location = new int[2];
        menuView.getLocationOnScreen(location);
        int menuViewX = location[0];
        int menuViewY = location[1];
    }
}

Presently, there is no documented and supported means for doing this, except for your own custom action layouts or action views. There is no API to retrieve ordinary contents of the action bar (home affordance, title, regular items).

ShowcaseView, as mentioned in the other answer, probably does some undocumented/unsupported things for this, such as traversing the View hierarchy to get at these widgets. That's risky, insofar as future versions of Android, or manufacturer/ROM modder changes to Android, may break that logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!