如何在Android中通过布局XML设置WebView的URL? [英] How do I set URL for WebView from layout XML in Android?

查看:198
本文介绍了如何在Android中通过布局XML设置WebView的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从布局main.xml设置WebView的URL.

I'm trying to set the URL for a WebView from the layout main.xml.

通过代码,很简单:

WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/index.html");

是否有一种简单的方法可以将此逻辑放入布局XML文件中?

Is there a simple way to put this logic into the layout XML file?

推荐答案

由于URL基本上是字符串,因此可以将其放入values/strings.xml文件

Since URL is basically a string, you can put it into values/strings.xml file

<resources>
    <string name="myurl">http://something</string>
</resources>

然后您可以像这样使用它:

then you can use it like this:

WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(getString(R.string.myurl));

这篇关于如何在Android中通过布局XML设置WebView的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆