我有一个FrameLayout,并在其中创建了一个加载一些URL的Web视图。然后将webview作为子级添加到FrameLayout中。我在Webview上实现了onTouch方法,但似乎只有framelayout可以检测到触摸。 here is how my screen is。橙色区域应检测到Web视图上的触摸,蓝色区域应检测到FrameLayout上的触摸。
这是我的代码:
this.post(new Runnable() {
@SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
@Override
public void run() {
try {
if (webView == null) {
webView = new WebView(context) {
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
// resize webView
LayoutParams layoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(50, 50, 50, 100);
webView.setLayoutParams(layoutParams);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(Color.TRANSPARENT);
}
current_url = webView.getUrl();
webView.setVisibility(View.VISIBLE);
if ( current_url == null || !overlay_url.contentEquals(current_url)) {
webView.loadUrl(overlay_url);
webView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
current_url = overlay_url;
}
if (indexOfChild(webView) == -1) {
addView(webView, 2);
}
} catch (Exception exc) {
Log.e(TAG, "exception in WebView", exc);
}
}