WebView点击不准确

时间:2013-03-19 15:53:48

标签: java android dialog android-webview

我找到了一个包含WebView的对话框的好类。 问题是,对话框中的每次单击都以某种方式向左移动。如果我点击屏幕的右上边缘,则表明点击位于屏幕中间的某个位置。 我试过让它变焦,但没有运气。任何人都可以解决这个问题吗?

PS:有一条可能相关的重复日志消息:

03-19 17:42:21.660: E/webcoreglue(10335): Should not happen: no rect-based-test nodes found

这是类代码:

public class WebDialog extends Dialog {
    static final int BLUE = 0xFF6D84B4;
    static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60};
    static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60};

    @SuppressWarnings("deprecation")
    static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

    static final int MARGIN = 4;
    static final int PADDING = 2;
    static final String DISPLAY_STRING = "touch";
    private String mUrl;
    // private DialogListener mListener;
    private ProgressDialog mSpinner;
    private WebView mWebView;
    private LinearLayout mContent;
    private TextView mTitle;

    public WebDialog(Context context, String url) {
        super(context);
        mUrl = url;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage(HebrewConstants.LOADING);
        mContent = new LinearLayout(getContext());
        mContent.setOrientation(LinearLayout.VERTICAL);
        mContent.setBackgroundColor(BLUE);
        setUpTitle();
        setUpWebView();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int orientation = getContext().getResources().getConfiguration().orientation;
        float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE)
                ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
        addContentView(mContent, new LinearLayout.LayoutParams(
                display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
                display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
    }

    private void setUpTitle() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        final ImageView img = new ImageView(getContext());
        Drawable icon = getContext().getResources().getDrawable(R.drawable.close);
        img.setImageDrawable(icon);
        img.setPadding(MARGIN + PADDING, MARGIN + PADDING, MARGIN + PADDING, MARGIN + PADDING);
        img.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        img.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    img.setImageDrawable(
                            getContext().getResources().getDrawable(R.drawable.close_focus));
                } else {
                    img.setImageDrawable(getContext().getResources().getDrawable(R.drawable.close));
                }
            }
        });
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.RIGHT;
        img.setBackgroundColor(BLUE);
        mTitle = new TextView(getContext());
        mTitle.setText(HebrewConstants.LOADING);
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setBackgroundColor(BLUE);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
        LinearLayout container = new LinearLayout(getContext());
        container.setOrientation(LinearLayout.HORIZONTAL);
        container.addView(img);
        RelativeLayout relative = new RelativeLayout(getContext());
        RelativeLayout.LayoutParams params = new LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        relative.addView(mTitle, params);
        container.addView(relative);
        mContent.addView(container);
    }

    private void setUpWebView() {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new WebDialog.DialogWebViewClient());
        // mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
        System.out.println(" mURL = " + mUrl);
        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mContent.addView(mWebView);
    }

    private class DialogWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description,
                String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            WebDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String title = mWebView.getTitle();
            if (title != null && title.length() > 0) {
                mTitle.setText(title);
            }
            mSpinner.dismiss();
        }    
    }
}

0 个答案:

没有答案