ViewGroupOverlay doesn't show view

余生颓废 提交于 2019-12-06 11:55:25

ViewGroupOverlay doesn't perform the layout pass on Views added to it; that is, as is automatically performed when adding a View to an existing layout, you need to manually perform measure() and layout() on a view in order for it to be correctly displayed on screen.

The reason your code works with the Drawable is because you are performing the layout step with myIcon.setBounds(0,0,100,100);

If you use this code you will get the same effect as your Drawable:

    View view = inflater.inflate(R.layout.overlay, null);
    view.measure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY));
    view.layout(0, 0, 100, 100);
    gridView.getOverlay().add(view);

You can read more about measure/layout here: https://developer.android.com/guide/topics/ui/how-android-draws.html

You can use this helper method to display a view as if it has been added to an empty full screen:

private static void measureAndLayout(Activity activity, View toMeasure, int statusBarHeight) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    toMeasure.measure(
            View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, View.MeasureSpec.EXACTLY) ,
            View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels - statusBarHeight, View.MeasureSpec.EXACTLY));

    toMeasure.layout(0, statusBarHeight, displayMetrics.widthPixels, displayMetrics.heightPixels);
}

You need to set setBottom, setRight, to your View you want to add to ViewGroupOverlay

Just like I wrote here http://uiandroid.com/reveal-effect-custom-color/

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