对话框中的 WebView(加载资源)未布局

7

我使用的是Android 2.2版本,并正在创建一个带有WebView的对话框:

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = new Dialog(this);
    //.....
    dialog.setContentView(R.layout.dialoghelp);
    WebView v = (WebView)dialog.findViewById(R.id.helpWebView);
    v.loadUrl("file:///android_asset/help.html");
    //......
    return dialog;
}

它能够工作,但是第一次打开对话框时,即使内容已经加载,WebView也没有布局。

我知道这是因为使用HierarchyViewer,我可以看到内容,并强制进行布局请求,我也可以在模拟器中看到它们。另外,如果我只是取消对话框并重新打开它,一切都可以正常工作。

谁出了问题,Android还是我?我尝试将加载放置在onPrepareDialog()中,但结果相同。

编辑

我将WebView的布局参数从fill_parent更改为wrap_content,这样就可以工作了。我可以看到它以0高度打开,然后在加载后变高。宽度甚至在之前就已经起作用。


我遇到了同样的问题,但将布局参数更改为“wrap_content”并没有帮助。你有其他的解决方案吗? - ductran
@R4j 我已经很久没有编写Android程序了,但我会尝试将“完成加载”事件附加到webview上,并在那里调用requestLayout() - bigstones
1个回答

1

我认为你可能需要覆盖Dialog类,类似于Facebook SDK已经实现的方式:

这里是他们的代码

public class FbDialog extends Dialog {

static final int FB_BLUE = 0xFF6D84B4;
static final float[] DIMENSIONS_DIFF_LANDSCAPE = {20, 60};
static final float[] DIMENSIONS_DIFF_PORTRAIT = {40, 60};
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";
static final String FB_ICON = "icon.png";

private String mUrl;
private DialogListener mListener;
private ProgressDialog mSpinner;
private ImageView mCrossImage;
private WebView mWebView;
private FrameLayout mContent;

public FbDialog(Context context, String url, DialogListener listener) {
    super(context, android.R.style.Theme_Translucent_NoTitleBar);
    mUrl = url;
    mListener = listener;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSpinner = new ProgressDialog(getContext());
    mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mSpinner.setMessage("Loading...");

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    mContent = new FrameLayout(getContext());

    /* Create the 'x' image, but don't add to the mContent layout yet
     * at this point, we only need to know its drawable width and height 
     * to place the webview
     */
    createCrossImage();

    /* Now we know 'x' drawable width and height, 
     * layout the webivew and add it the mContent layout
     */
    int crossWidth = mCrossImage.getDrawable().getIntrinsicWidth();
    setUpWebView(crossWidth / 2);

    /* Finally add the 'x' image to the mContent layout and
     * add mContent to the Dialog view
     */
    mContent.addView(mCrossImage, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    addContentView(mContent, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}

private void createCrossImage() {
    mCrossImage = new ImageView(getContext());
    // Dismiss the dialog when user click on the 'x'
    mCrossImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onCancel();
            FbDialog.this.dismiss();
        }
    });
    Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.close);
    mCrossImage.setImageDrawable(crossDrawable);
    /* 'x' should not be visible while webview is loading
     * make it visible only after webview has fully loaded
    */
    mCrossImage.setVisibility(View.INVISIBLE);
}

private void setUpWebView(int margin) {
    LinearLayout webViewContainer = new LinearLayout(getContext());
    mWebView = new WebView(getContext());
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.setWebViewClient(new FbDialog.FbWebViewClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl(mUrl);
    mWebView.setLayoutParams(FILL);
    mWebView.setVisibility(View.INVISIBLE);
    mWebView.getSettings().setSavePassword(false);

    webViewContainer.setPadding(margin, margin, margin, margin);
    webViewContainer.addView(mWebView);
    mContent.addView(webViewContainer);
}

private class FbWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Util.logd("Facebook-WebView", "Redirect URL: " + url);
        if (url.startsWith(Facebook.REDIRECT_URI)) {
            Bundle values = Util.parseUrl(url);

            String error = values.getString("error");
            if (error == null) {
                error = values.getString("error_type");
            }

            if (error == null) {
                mListener.onComplete(values);
            } else if (error.equals("access_denied") ||
                       error.equals("OAuthAccessDeniedException")) {
                mListener.onCancel();
            } else {
                mListener.onFacebookError(new FacebookError(error));
            }

            FbDialog.this.dismiss();
            return true;
        } else if (url.startsWith(Facebook.CANCEL_URI)) {
            mListener.onCancel();
            FbDialog.this.dismiss();
            return true;
        } else if (url.contains(DISPLAY_STRING)) {
            return false;
        }
        // launch non-dialog URLs in a full browser
        getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        mListener.onError(
                new DialogError(description, errorCode, failingUrl));
        FbDialog.this.dismiss();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        Util.logd("Facebook-WebView", "Webview loading URL: " + url);
        super.onPageStarted(view, url, favicon);
        mSpinner.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        mSpinner.dismiss();
        /* 
         * Once webview is fully loaded, set the mContent background to be transparent
         * and make visible the 'x' image. 
         */
        mContent.setBackgroundColor(Color.TRANSPARENT);
        mWebView.setVisibility(View.VISIBLE);
        mCrossImage.setVisibility(View.VISIBLE);
    }
}

}


@bigstones - 所以我发布的示例没有任何特定的行来解决您的问题。首先,onCreateDialog已被弃用,在您尝试使用Web视图设置对话框内容的活动中,请尝试覆盖对话框类,并在其中将内容设置为Web视图。 - Aditya Pratap Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接