如何从Android上的XML布局文件上的WebView设置网址是什么?布局、网址、文件、Android

2023-09-05 10:04:36 作者:杀无赦

我试着去设置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));