3

I have a layout below where i have a linear layout with gravity as center to make the child center aligned.I want to add a webview programatically and loading the youtube video in it.The issue is the webview's height and width is wrap_content,wrap_content.So in portrait mode webview works fine and aligns at center but when the change the orientation to landscape web view becomes fill_parent and covers the screen horizontally so the content doesn't look at center. here is the layout file

and here the activity code

Below are the image which shows result in portrait mode which is fine. Linear layout is in green color and webview is in blue color

and here is when u rotate the device to landscape mode where webview in blue will stretch itself to fill the entire screen width wise.

enter image description here

I want the webview to be placed center in landscape mode also likewise it is in portrait mode?Please help me understanding this and any fix?

4

4 回答 4

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 应该用新的坐标刷新。如果你对此感兴趣,我可以分享更多细节。

于 2013-10-16T13:14:04.573 回答
1

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

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。

于 2018-07-19T10:30:25.573 回答
0

尝试这个

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

于 2013-09-06T11:32:57.330 回答
0

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

于 2013-09-06T11:01:42.077 回答