简体   繁体   English

如何设置图片/ WebView的高度/宽度以匹配父级?

[英]How to set image/WebView height/width to match the parent?

Hello all and good day. 大家好,美好的一天。

I found it easy to display GIF animations using WebView as it is fairly easy and simple. 我发现使用WebView轻松显示GIF动画非常简单。

Now the GIF I'm displaying is of the size 800x800px. 现在,我要显示的GIF尺寸为800x800px。 What I want is that this GIF to fit the screen available to it, possibly to match parent something like that, so that one does not have to scroll or zoom in/out the picture. 我想要的是此GIF可以适合其可用的屏幕,可能与父级匹配,这样一来,不必滚动或放大/缩小图片。 It just fits automatically on any screen available to it. 它只是自动适合任何可用的屏幕。

My device used for experimenting is: GT-I9100/Samsung Galaxy SII which has the screen size of 800x480px. 我用于实验的设备是:GT-I9100 / Samsung Galaxy SII,其屏幕尺寸为800x480px。

Here's a snippet of code from my main activity: 这是我主要活动中的一段代码:

HToad view = new HToad(this, "file:///android_asset/hypnotoad.gif");
view.setInitialScale(30);
view.getSettings().setUseWideViewPort(true);
setContentView(view);

This is the HToad class: 这是HToad类:

import android.content.Context;
import android.webkit.WebView;

public class HToad extends WebView {

    public HToad(Context context, String path) {
        super(context);
        // TODO Auto-generated constructor stub
        loadUrl(path);
    }

}

I tried using various settings like LayoutParams and IntialSize etc, none of which helped me to achieve what I want. 我尝试使用各种设置,例如LayoutParams和IntialSize等,这些都无法帮助我实现所需的设置。

I'm using JAVA for WebView, not defining WebView in XML. 我正在将JAVA用于WebView,而不是在XML中定义WebView。

Thank you in advance. 先感谢您。

Try to load your gif with the loadData method instead loadUrl and add a css style like this: 尝试使用loadData方法而不是loadUrl加载gif并添加如下的css样式:

<style type='text/css'>
    img {max-width: 100%;height:initial;}
</style>

And in the end you must get something like this: 最后,您必须获得以下内容:

setBackgroundColor(Color.TRANSPARENT);
loadData("<style type='text/css'> img {max-width: 100%;height:initial;}</style>" +
         "<img align='middle' src='file:///android_asset/hypnotoad.gif'/>", "text/html", "utf-8");
reload();

I hope I helped you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM