Setting webview at the center of relative layout

拜拜、爱过 提交于 2019-12-02 01:36:35

问题


I am setting following property for webview

    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);

and using following layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/solid_white"
    android:layout_marginLeft="1dp" android:gravity="center">


     <WebView
        android:id="@+id/MyView"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </WebView> 

</RelativeLayout>

This layout is for fragment. View is coming centrally vertical but it not coming centrally horizontal. What I need to do to set webview at the center of relative layout?


回答1:


Actually Android WebView's wrap content layout paramter fill the parent width, and I believe its coz it is being optimized to use all the screen width, so you need to set some fixed width, to show in center




回答2:


Just try this:

MyView.getSettings().setLoadWithOverviewMode(true);
MyView.getSettings().setUseWideViewPort(true);

I hope it works for you.




回答3:


In Relative Layout attribute change like this :

android:gravity="center_vertical|center_horizontal"

No need to add android:layout_centerInParent="true" in WebView Hope it will work.



来源:https://stackoverflow.com/questions/9938464/setting-webview-at-the-center-of-relative-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!