android - 对话框中的 WebView(加载 Assets )且未布局

标签 android dialog webview

我在 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 高度打开,然后在加载后长大。宽度甚至在以前就起作用了。

最佳答案

好吧,我认为您可能需要重写 Dialog 类,类似于 facebook sdk 已经实现的类:-

这里有代码

公共(public)类 FbDialog 扩展对话框 {

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);
    }
}

关于android - 对话框中的 WebView(加载 Assets )且未布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5366330/

相关文章:

android - 如何以编程方式将主题设置为android对话框?

jQuery 对话框按钮 - 如何将目录设置为 rtl/ltr 或者如何将按钮放在对话框的左上角

android - 使用动态大小将 div 对齐到页面中心

android - 如何在 WallpaperService 中设置 webview 的大小?

Dropbox Chooser 中未显示 Android 应用程序

android - 我对 android 上的 HttpURLConnections 的问题。 (cast-URLConnection, url.openConnection, getOutPutStream()..)

android - getView() 的两种实现有什么区别?

java - 更新 MutableLiveData 列表

android - 如何去除对话框中的白色边框?

java - WebView 无法从 wordpress 网站下载媒体(POST)