如何从Android上的XML布局文件上的WebView设置网址是什么? [英] How to set url on WebView from a xml layout file on Android?

查看:197
本文介绍了如何从Android上的XML布局文件上的WebView设置网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去设置URL从布局main.xml中一个web视图。

Im trying to set the url on a WebView from the layout main.xml.

通过code很简单:

    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基本上是一个字符串,你可以把它变成价值/ 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设置网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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