我正在使用 Android Webview,我的 html 文件位于 assets 目录中。 我的 html 中有一个 href:
<a class='edit link1'></a>
现在,我想在按下 android 按钮小部件时使用我的 Java 代码动态更改此代码,并向其添加一些样式,例如:
<a class='edit link1' style="border-radius:20px;"></a>
有可能吗?提前致谢。
下面是我的 webView:
wv1 = (WebView) findViewById(R.id.webview);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.getSettings().setLoadWithOverviewMode(true);
wv1.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
wv1.setScrollbarFadingEnabled(false);
wv1.getSettings().setBuiltInZoomControls(true);
wv1.getSettings().setPluginState(WebSettings.PluginState.ON);
wv1.getSettings().setAllowFileAccess(true);
wv1.addJavascriptInterface(new WebViewJavaScriptInterface(this), "Android");
wv1.getSettings().setSupportZoom(true);
wv1.setWebViewClient(new MyBrowser());
wv1.loadUrl("file:///android_asset/a.html");
最佳答案
Is it even possible? No, Not Possible.
Android assets 文件是一种资源文件,一旦构建了 apk 文件就无法修改它们。
solution for your problem -
利用存储文件系统。 (非常糟糕的方法,因为读取、写入、加载操作将在那里)。
使用 java 脚本接口(interface)并尝试动态地向 html 元素添加样式。(理想的)
示例:Changing element style attribute dynamically using JavaScript
关于java - 使用 webview 将 HTML 代码添加到 Assets 文件夹中的 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46998720/