【问题标题】:WebView Orientation change issueWebView 方向更改问题
【发布时间】:2013-09-06 10:38:40
【问题描述】:

我在下面有一个布局,其中我有一个以重力为中心的线性布局,以使子中心对齐。我想以编程方式添加一个 webview 并在其中加载 youtube 视频。问题是 webview 的高度和宽度是 wrap_content ,wrap_content.So 在纵向模式下,webview 工作正常并在中心对齐,但是当将方向更改为横向 web 视图时变为 fill_parent 并水平覆盖屏幕,因此内容不会看中心。 here is the layout file

here the activity code

下面是在纵向模式下显示结果的图像,这很好。 线性布局为绿色,webview 为蓝色

当你将设备旋转到横向模式时,蓝色的 webview 将自行拉伸以填充整个屏幕宽度。

我希望将 webview 置于横向模式的中心,同样它也处于纵向模式?请帮助我理解这一点并进行任何修复?

【问题讨论】:

    标签: android-layout android-webview


    【解决方案1】:

    如果你在 WebView 中加载视频,那么 WebView 中的对齐是 html 任务。将您的视频包装在 <div> 中并添加适当的样式(在 css 文件中或标签中的 style 属性中):

    <div style="margin:0 auto;width:500px;">
        //your video object is placed here
    </div>
    

    您也可以在 VideoView 中播放视频,但这种技术会有点困难。简而言之,您必须在 WebView 上方添加一个 FrameLayout,通过 html 文件获取 VideoView 应放置的坐标,并为 VideoView 应用带有坐标的 LayoutParams。如果方向会改变,VideoView 应该用新的坐标刷新。如果您对此感兴趣,我可以分享更多详细信息。

    【讨论】:

      【解决方案2】:

      好吧,我可能为时已晚。但是,我做什么:

      AndroidManifest.xml

      android:configChanges="orientation"

      WebViewContainerActivity.java

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          /* WebView mainView is declared globally */
          ConstraintLayout lana = findViewById(R.id.main);
          lana.removeView(mainView);
          DisplayMetrics displaymetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
          int screenWidth = displaymetrics.widthPixels;
          int screenHeight = displaymetrics.heightPixels;
          lana.addView(mainView, 0,
                  new ConstraintLayout.LayoutParams(
                          screenWidth,
                          screenHeight));
          super.onConfigurationChanged(newConfig);
      }
      

      它的工作效率很高,我必须使用它,因为我不懂 HTML。

      【讨论】:

        【解决方案3】:

        为什么不用wrap_content 设置横向布局的宽度,然后将layout_gravity 设置为"center"

        【讨论】:

        • 您还希望我为横向模式创建单独的布局吗?请详细说明。
        • 我认为这就是你已经在做的事情,但如果不是,那么是的。
        • 另一种解决方案是简单地将 WebView 上的布局重力设置为居中,因为您将其添加到线性布局中。
        • 另外,您知道您可以从 xml 资源中扩充 WebView,因此您不必在代码中设置所有参数。其实我不知道你为什么不直接把WebView放到你的布局文件中,然后在Activity中通过它的id找到它。
        • 这只是一个示例。在实际项目中实现您建议的相同内容。
        【解决方案4】:

        试试这个

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center"  >
        <LinearLayout
           android:id="@+id/webViewPlaceholder"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="#00AC0F"
           >
        </LinearLayout>
        

        【讨论】:

        • 第二个线性布局需要android:layout_gravity="center"。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 2023-03-24
        • 2014-10-30
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        相关资源
        最近更新 更多