问题
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:
In each 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);
}
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...
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.
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);
In general, the Overlay-technique added in API18 seems to be never used. Any experts here?
回答1:
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);
}
回答2:
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/
来源:https://stackoverflow.com/questions/31862961/viewgroupoverlay-doesnt-show-view