WebView在方向更改时无法正确调整大小 [英] WebView not re sizing properly on orientation change

查看:122
本文介绍了WebView在方向更改时无法正确调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用webview.loadUrl()将视频网址加载到webview中.播放器是用html本身编码的,因此我只需要将URL加载到Web视图中即可.

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.

如果我在清单中没有给出android:configChanges ="orientation",则一切正常.更改方向后,网络视图的大小会适当调整,视频会调整到适合屏幕的大小,然后视频会重新启动.

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.

因此,我在清单中给出了android:configChanges ="orientation",以使视频不会重新加载.现在,尽管视频无法重新加载并继续播放,但是如果设备最初是纵向的,然后变为横向的,则视频/网络视图的大小无法正确调整,并且视频的一半会显示在屏幕之外.如果设备最初处于横向模式,然后更改为纵向模式,则仅使用一半的屏幕.

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.

推荐答案

* 如果configchanges ="orientation"-未指定 * android系统会处理更改并重新启动布局,因此将重新创建布局并从一开始就加载视频.

*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.

如果android:configchanges ="orientation" ,- 这告诉系统该活动将自行处理方向更改. 因此,Android系统不会刷新加载布局.因此视频会继续播放.父版面的宽度不会改变.因此纵向模式下的宽度用于横向.因此,您的网络视图似乎很小.

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.

您需要在活动中覆盖onconfigurationchanged()并处理方向更改. 您需要指定用于布局的layoutparams.

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

这篇关于WebView在方向更改时无法正确调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆