问题
I am loading a video url into webview using webview.loadUrl(). The player is encoded in the html itself so I only have to load the url into web view.
In case if I dont give android:configChanges="orientation" in my manifest everything works fine. The webview resizes properly on orientation change and the video is adjusted to fit the screen properly, ofcourse then the video restarts.
Therefore I gave android:configChanges="orientation" in manifest so that the video doesnt reload. Now although the video doesn't reload and continues to play but the video/webview doesnt resize properly and half of the video goes off screen if the device was initially in portrait and then changed to landscape. In case the device was initially in landscape mode and then is changed to portrait mode only half of the screen is used.
Please reply if you can figure out a way or have any suggestions.
回答1:
*if configchanges = "orientation" - not specified,* the android system handles the change and restarts the layout, so layout is freshly created and video loads from start.
if android:configchanges = "orientation" , - This tell the system that this activity will handle the orientation change by it self. so,the android system will not load the layout refreshly . so video continues to play .The width of the parent layout will not change . so width in portrait mode is used in landscape . so , ur webview seems small.
you need to override the onconfigurationchanged () in activity and handle the orientation change. You need to specify the layoutparams for layout.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// for example the width of a layout
int width = 300;
int height = LayoutParams.WRAP_CONTENT;
WebView childLayout = (WebView) findViewById(R.id.webview);
childLayout.setLayoutParams(new LayoutParams(width, height));
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
回答2:
Try:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
webView.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
回答3:
Just add the following line to your activity in the manifest file:
<activity android:configChanges="orientation|screenSize">
回答4:
facing same issue. and solve it with best way
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
mWebview.setLayoutParams(new LinearLayout.LayoutParams(
width,
height-100));
}
回答5:
As far as I can see, there must be some error in H5 page, similar resize problem
Just use other app (like chrome browser) test your url.
回答6:
In fragments you have to use FrameLayout for WebView
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int width = FrameLayout.LayoutParams.MATCH_PARENT;
int height = FrameLayout.LayoutParams.WRAP_CONTENT;
your_webview.setLayoutParams(new FrameLayout.LayoutParams(width, height));
}
来源:https://stackoverflow.com/questions/16419735/webview-not-re-sizing-properly-on-orientation-change