在MediaController顶部添加视图 [英] Add view on top of MediaController
问题描述
MediaController始终显示在屏幕上所有视图的顶部,并且在隐藏之前不要将点击传递给下面的视图,我的问题是:
MediaController always shown on top of all views in the screen and do not pass clicks to below views until it is hidden, my issue is:
如何在MediaController顶部添加按钮视图,以便其处理单击事件? 或者 如何将点击事件传递到下面的视图?
How to add button view on top of MediaController so it handle click events ? OR How to pass click events to below view?
推荐答案
Try overriding MediaController.dispatchTouchEvent()
它更适合您的任务,请在我的回答此处详细了解原因.
It suits your task better, see my answer here for a detailed explanation why.
代码将如下所示:
public class MyMediaController extends MediaController {
...
private WeakReference<Button> mButton;
// TODO You'll have to call this whenever the layout might have been rebuilt
// i.e. in onCreate() of your activity (or in onConfigurationChanged() if you
// handle configuration changes yourself)
public void setButton(Button button) {
mButton = new WeakReference<Button>(button);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int iX = (int) event.getX();
int iY = (int) event.getY();
Button button = mButton.get();
if (button != null) {
Rect buttonHitRect = new Rect();
button.getHitRect(buttonHitRect);
if (buttonHitRect != null && buttonHitRect.contains(iX, iY)) {
// user hit the button, dispatch event to the button
button.dispatchTouchEvent(event);
return true;
}
}
// button did not get hit, pass the touch through
return super.dispatchTouchEvent(event)
}
}
尝试一下,让我知道如何进行.
Try this, let me know how it goes.
UPD Theres's a very similar question on this, it may have a better solution: Android MediaController intercepts all other touch events
这篇关于在MediaController顶部添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!