【问题标题】:Load image from url in WebView ( center vertically )从 WebView 中的 url 加载图像(垂直居中)
【发布时间】:2016-01-27 12:21:51
【问题描述】:

我会尝试在 webview 中加载图像,但图像位于顶部,我需要它们的方向是垂直居中。

这是代码:

。 . .

mStringUrl = getIntent().getExtras().getString(ARGS_IMAGE_URL);

。 . .

private void initializeWebView() {

    mWebView.loadDataWithBaseURL(null, "<html><head><style>img {margin-top:auto;margin-bottom:auto}</style></head><body><img src=\"" + mStringUrl + "\"></body></html>", "html/css", "utf-8", null);

    WebSettings settings = mWebView.getSettings();
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

    mWebView.setBackgroundColor(getResources().getColor(R.color.transparent));

    mWebView.setWebViewClient(new CustomWebViewClient());

}

xml 文件:

<include layout="@layout/layout_toolbar" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:background="@color/black_midnight" />

    <ProgressBar            
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:visibility="gone"
        style="@android:style/Widget.Material.ProgressBar.Small"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

如果有任何想法请帮助我,在此先感谢。

【问题讨论】:

    标签: android html css webview


    【解决方案1】:

    试试这个代码:

    mWebView.loadDataWithBaseURL(null, "<html><head></head><body><table style=\"width:100%; height:100%;\"><tr><td style=\"vertical-align:middle;\"><img src=\"" + mStringUrl + "\"></td></tr></table></body></html>", "html/css", "utf-8", null);
    

    【讨论】:

      【解决方案2】:

      试试这个:

         settings().setDefaultZoom(ZoomDensity.FAR);
      

      【讨论】:

      • 发布包含 WebView 的 xml
      • API 版本 19 中已弃用
      猜你喜欢
      • 2014-06-18
      • 2010-12-30
      • 2020-03-16
      • 2012-02-27
      • 1970-01-01
      • 2013-03-04
      • 2014-04-28
      相关资源
      最近更新 更多