ViewGroupOverlay不显示视图 [英] ViewGroupOverlay doesn't show view

查看:299
本文介绍了ViewGroupOverlay不显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个益智游戏,其中有一个网格,每个单元应该能够在触摸时显示视觉(且仅视觉)指示.因此,我打算将<​​a href="https://developer.android.com/reference/android/view/ViewGroupOverlay.html" rel="nofollow"> ViewGroupOverlay (与getOverlay()一起)一起使用自定义视图:

I'm currently trying to create a puzzle game where I have a grid and each cell should be able to show a visual (and only visual) indication on touch. Therefore, I intent to use the ViewGroupOverlay (with getOverlay()) with a custom view:

在每个 CellView

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View myView = inflater.inflate(R.layout.overlay, null);
            gridView.getOverlay().add(myView);
        }
        if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            gridView.getOverlay().clear();
        }
        return super.onTouchEvent(event);
    }

其中'gridView'是父级(一个包含一个GridLayout的FrameLayout,该GridLayout包含多个CellViews(它们是RelativeLayout));但是我认为那并不重要...

where 'gridView' is the parent (a FrameLayout containing a GridLayout that holds the multiple CellViews (which are RelativeLayout)) ;). But I think that doesn't really matters...

当前用于 overview.xml (仅是伪代码)

And currently for the overview.xml (Just dummy code)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dddd090d">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Test" />

</LinearLayout>

遗憾的是,叠加视图没有出现在任何地方.

Sadly, the overlay view doesn't appear anywhere.

但是,如果我尝试使用可绘制对象并在"onTouchEvent"中使用以下代码,则有效 ...但是我会需要视图而不是可绘制对象来工作...

However, if I try it with a drawable and the following code in 'onTouchEvent', it works... But I would need the view and not the drawable to work...

Drawable myIcon = getResources().getDrawable(R.drawable.overlay);
myIcon.setBounds(0,0,100,100);
gridView.getOverlay().add(myIcon);

通常,似乎从未使用过API18中添加的Overlay技术.这里有专家吗?

In general, the Overlay-technique added in API18 seems to be never used. Any experts here?

推荐答案

ViewGroupOverlay不会对添加到其中的视图执行布局传递;也就是说,就像在将视图添加到现有布局时自动执行的那样,您需要在视图上手动执行measure()layout(),以使其正确显示在屏幕上.

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.

您的代码与Drawable一起使用的原因是因为您正在使用myIcon.setBounds(0,0,100,100);

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

如果使用此代码,您将获得与Drawable相同的效果:

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);

您可以在此处阅读有关度量/布局的更多信息: https: //developer.android.com/guide/topics/ui/how-android-draws.html

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);
}

这篇关于ViewGroupOverlay不显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆