WebView Orientation change issue

后端 未结 4 672
小鲜肉
小鲜肉 2021-01-20 11:12

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 vi

相关标签:
4条回答
  • 2021-01-20 11:30

    Why not wrap_content for width in the landscape layout and then set the layout_gravity to "center"?

    0 讨论(0)
  • 2021-01-20 11:32

    Try this

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

    0 讨论(0)
  • 2021-01-20 11:44

    If you load video in WebView, so aligment in the WebView is html task. Wrap your video in the <div> and add appropriate style (in the css file or as style attribute in the tag):

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

    As well you can play a video in the VideoView but this is technique will be a little more difficult. Briefly, you have to add a FrameLayout above the WebView, get the coords where the VideoView should be placed through the the html file and apply LayoutParams with coords for VideoView. If orientation will be changed, VideoView should be refreshed with the new coords. If you interested in this, i can share more details.

    0 讨论(0)
  • 2021-01-20 11:46

    Well, I may be too late. But, what I do :

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

    It does it's work efficiently, and I must use this because I don't understand HTML.

    0 讨论(0)
提交回复
热议问题