如何在android中控制webview景观和肖像?

时间:2012-12-04 11:48:23

标签: android webview

在我的应用程序中,我使用webview来显示html页面。 Html页面包含YouTube视频。    在纵向模式下,它正在加载并正常工作。但是当方向更改为横向模式时,页面将再次加载(重新创建),并且html页面中的视频仅显示一半大小。我在清单文件中使用了android:configChanges =“orientation”但是没有工作......并且当取向时视频应该继续

这是我的代码......,

         webb=(WebView)findViewById(R.id.weeeb1);
                 webb.getSettings().setJavaScriptEnabled(true);
                    webb.setWebChromeClient(new WebChromeClient() {
                        public void onProgressChanged(WebView view, int progress)
                        {}
                    });

                    webb.setWebViewClient(new WebViewClient() {
                        @Override
                        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
                        {
                            // Handle the error
                        }

                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url)
                        {
                            view.loadUrl(url);
                            return true;
                        }
                    });
                    webb.getSettings().setPluginsEnabled(true);
                    //webb.setInitialScale(12);
                    webb.getSettings().setLoadWithOverviewMode(true);
                    webb.getSettings().setUseWideViewPort(true);
                    webb.setPadding(0,0,0,0); 
                    webb.loadUrl("file:///android_asset/index1.html");

Portrait mode

    <ScrollView android:id="@+id/row33"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/titletext"
          android:fillViewport="false"
          android:scrollbars="none"
          android:layout_weight="1"
          >
        <LinearLayout
          android:id="@+id/row35"
          android:layout_width="fill_parent"
          android:layout_height="600dp"
          android:layout_below="@+id/row33"
          android:layout_margin="8dp" 
          android:orientation="vertical" 
          android:layout_weight="1"
          >
    <LinearLayout
          android:id="@+id/row34"
          android:layout_width="fill_parent"
          android:layout_height="260dp"
          android:layout_below="@+id/row33"
          android:layout_marginLeft="6px"
          android:layout_marginRight="17px"
          android:orientation="horizontal" 
          android:layout_weight="1"
          android:background="#1C2F69"
          >


        <ImageView
            android:id="@+id/gimage1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_marginTop="10px"
            android:layout_marginLeft="30px"
            android:layout_marginRight="10px"
            android:layout_marginBottom="10px"
            android:background="@drawable/natureimage4"
            />

        <ImageView
            android:id="@+id/gimage2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_marginTop="10px"
            android:layout_marginLeft="10px"
            android:layout_marginRight="30px"
            android:layout_marginBottom="10px"
            android:background="@drawable/natureimage5"
            />

     </LinearLayout>
      <WebView 
         android:id="@+id/weeeb1" 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
          android:layout_marginLeft="6px"
          android:layout_marginRight="17px"
          android:layout_marginTop="5px"
          android:layout_marginBottom="29dp"
         android:layout_below="@+id/row35"
          android:layout_weight="1"
         />
         </LinearLayout>
     </ScrollView>


Landscape mode

<ScrollView android:id="@+id/row33"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/titlemain"
      android:fillViewport="false"
      android:scrollbars="none"
      android:layout_weight="1">
    <LinearLayout
      android:id="@+id/row35"
      android:layout_width="fill_parent"
      android:layout_height="600dp"
      android:layout_below="@+id/row33"
      android:layout_marginLeft="190dp" 
      android:layout_marginRight="190dp"
      android:layout_marginTop="5dp"
      android:orientation="vertical" 
      android:layout_weight="1">
<LinearLayout

          android:id="@+id/row34"
          android:layout_width="fill_parent"
          android:layout_height="200dp"
          android:layout_below="@+id/row33"
          android:orientation="horizontal" 
          android:layout_weight="1"
          android:background="#1C2F69">


        <ImageView
            android:id="@+id/gimage1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_marginTop="10px"
            android:layout_marginLeft="35px"
            android:layout_marginRight="25px"
            android:layout_marginBottom="10px"
            android:background="@drawable/natureimage4" />

        <ImageView
            android:id="@+id/gimage2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_marginTop="10px"
            android:layout_marginLeft="25px"
            android:layout_marginRight="35px"
            android:layout_marginBottom="10px"
            android:background="@drawable/natureimage5" />

     </LinearLayout>
      <WebView 
         android:id="@+id/weeeb1" 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="9dp"
         android:layout_below="@+id/row35"
         android:layout_weight="1" />
         </LinearLayout>
     </ScrollView>


Please suggest me the solution....

2 个答案:

答案 0 :(得分:1)

一种解决方案是避免轮换。添加此项以仅在清单中修复纵向方向的活动。

android:screenOrientation="portrait"

答案 1 :(得分:0)

在清单中使用此功能。它可能对你有帮助。

android:configChanges="keyboardHidden|orientation|screenSize"