Android WebView 宽度

发布于 2024-12-23 07:16:40 字数 474 浏览 3 评论 0原文

我正在尝试控制 WebView 的宽度比例。 经过整整两天的时间。我仍然停留在我开始的地方。 我已遵循以下文档: http://developer.android.com/guide/webapps/targeting.html http://developer.android.com/guide/practices/screens_support.html

我已经尝试过元标记、css、WebView.setInitialScale。 有时,这些更改会使 WebView 剪掉屏幕的中间部分,有时甚至剪掉三分之一。 当您认为自己在疯狂中找到了可预测的模式时,您会发现布局响应完全是随机的。 有谁知道是否有办法在 Android 平台上实现嵌入式 HTML5 网页,还是只是 Android 平台尚未准备好?

I'm trying to control the width scale of a WebView.
After spending about two full days. I'm still left where i started.
I have followed the following documentation:
http://developer.android.com/guide/webapps/targeting.html
http://developer.android.com/guide/practices/screens_support.html

I have experimented with the meta tags, css, WebView.setInitialScale.
Sometimes the changes makes the WebView clip the middle of the screen out, sometimes one third.
When you think you have found a predictable pattern in the madness, you find out that layout response is total random.
Does anybody know if there is a way forward implementing embedded HTML5 web pages on the Android platform or is it just something that the Android platform isn't ready for yet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-12-30 07:16:40

我不确定你到底哪里出了问题,但这里有一些我用来测试嵌入式 WebView 的样板代码。我应该提到的是,我主要针对 Honeycomb (3.x),但您没有指定:

这是简单的示例布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</FrameLayout>

现在这是 Activity 支持者的代码:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.Toast;

public class WebViewTestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

    final String TAG = "webview test";

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);     

    FrameLayout vg = (FrameLayout) findViewById(R.id.FrameLayout1);
    WebView webView = new WebView(this);

    vg.removeAllViews();
    vg.addView(webView);

    WebSettings settings = webView.getSettings();           
    settings.setSupportZoom(false);
    settings.setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onConsoleMessage(ConsoleMessage cm) {

            Log.i(TAG, cm.messageLevel() + ": " + cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId() );

            return true;
        }
    });

    webView.addJavascriptInterface(new Object() {

        @SuppressWarnings("unused")
        public void foo() {
            Log.i(TAG, "----- FOO CALLED -----");
            Toast.makeText(WebViewTestActivity.this, "foo called", Toast.LENGTH_LONG).show();
        }

    }, "android");

    String html = "<!DOCTYPE html><html><body style=\"background:black;color:white;padding:20px;\"><a href=\"javascript:android.foo()\">CLICK ME!</a><div><label>HTML5 number input: <input type=number /></label></div></body></html>";
    webView.loadDataWithBaseURL("http://yourOptionalBaseUrl.com/", html, "text/html", "utf-8", null);   
}        
}

...这就是您需要嵌入 html5 页面的全部内容。您还可以将 html 页面放入“assets”文件夹中并使用以下方式加载它:

webView.loadUrl("file:///android_asset/page.html");

或者仅嵌入单个资源,例如

或使用之前相同的加载 URL 语法将其指向您的服务器

webView.loadUrl("http://www.yourpage.com/android?param=custom");

...我希望这会有所帮助...

I'm not sure where exactly you went wrong but here's some boilerplate code I use for testing my embedded WebViews. I should mention that I mainly target Honeycomb (3.x), but you didnt' specify so:

Here's the simple sample layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</FrameLayout>

Now here's the code for the Activity backer:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.Toast;

public class WebViewTestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

    final String TAG = "webview test";

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);     

    FrameLayout vg = (FrameLayout) findViewById(R.id.FrameLayout1);
    WebView webView = new WebView(this);

    vg.removeAllViews();
    vg.addView(webView);

    WebSettings settings = webView.getSettings();           
    settings.setSupportZoom(false);
    settings.setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onConsoleMessage(ConsoleMessage cm) {

            Log.i(TAG, cm.messageLevel() + ": " + cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId() );

            return true;
        }
    });

    webView.addJavascriptInterface(new Object() {

        @SuppressWarnings("unused")
        public void foo() {
            Log.i(TAG, "----- FOO CALLED -----");
            Toast.makeText(WebViewTestActivity.this, "foo called", Toast.LENGTH_LONG).show();
        }

    }, "android");

    String html = "<!DOCTYPE html><html><body style=\"background:black;color:white;padding:20px;\"><a href=\"javascript:android.foo()\">CLICK ME!</a><div><label>HTML5 number input: <input type=number /></label></div></body></html>";
    webView.loadDataWithBaseURL("http://yourOptionalBaseUrl.com/", html, "text/html", "utf-8", null);   
}        
}

...and that's all you need to have an embedded html5 page. You can also put your html page into your "assets" folder and load it using:

webView.loadUrl("file:///android_asset/page.html");

or embed just a single resource like

or just point it to your server using the same load URL syntax from before

webView.loadUrl("http://www.yourpage.com/android?param=custom");

... i hope this helps...

笑咖 2024-12-30 07:16:40

我得出的结论是,修改 WebView 宽度的唯一方法是操纵 html 元标记中找到的目标密度dpi

通过将目标密度 dpi 设置为我的设备宽度,我可以使网页跨越屏幕的整个宽度。
通过在运行时创建 html 文件,我可以用检测到的设备宽度填充目标密度dpi。哎呀!!

I have reached the conclusion that the only way to modify the WebView width is to manipulate the target-densitydpi found in the html meta tag

By setting the target-densitydpi to my device width i get the web page to span the entire width of the screen.
By creating the html file at runtime i can fill in the target-densitydpi with the detected device width. Yikes!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文