繁体   English   中英

WebView方向更改未调整页面大小

[英]WebView Orientation Change not resizing Webpage

我正在制作一个android应用程序,如果我位于WebView的最顶端,并且尝试将手机切换为横向模式,则它不会调整网页大小以适合手机屏幕。 它只会占据屏幕的一半,但是如果我向下滚动一点并切换到横向模式,它就可以正常工作!

我在Android清单中有android:configChanges="orientation" ,以防止在我更改方向时不会重置android:configChanges="orientation"

^^注意^^
即使您距页面顶部只有十分之一英寸,它也可以正常工作,但是如果您一直位于页面顶部,它将无法正常工作。

这是类有问题的类:

package my.app.name;

import my.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ConverterCatalogActivity extends Activity {
    WebView browser;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Set the Content View */
        setContentView(R.layout.main);

        /* Get the WebView */
        WebView wv1 = (WebView) findViewById(R.id.wv1);

        /* Activate JavaScript */
        wv1.getSettings().setJavaScriptEnabled(true);
        wv1.canGoBack();
        browser=(WebView)findViewById(R.id.wv1);
        browser.loadUrl("file:///android_asset/index.html");

        /* Prevent WebView from Opening the Browser */
        wv1.setWebViewClient(new InsideWebViewClient());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // We do nothing here. We're only handling this to keep orientation
        // or keyboard hiding from causing the WebView activity to restart.
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
        if(browser.canGoBack()){
                browser.goBack();
                return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}


/* Class that prevents opening the Browser */
private class InsideWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

这是横向模式下的屏幕截图。

我以前在使用WebView时遇到问题。 尝试在OnOrientationChanged测量视图,这可能会解决您的问题!

@Override
public void onSaveInstanceState(Bundle outState) {
    ((WebView) mView.findViewById(R.id.webView)).saveState(outState);
}

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    mWebView.restoreState(savedInstanceState);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM