英文:
Is there any way to exactly implement a "desktop mode" function into my mobile app when loading website content?
问题
有没有办法在加载网站内容时将“桌面模式”功能精确地实现到我的移动应用程序中?我正在制作一个Android应用程序,我想要一个页面,只包含静态网站内容,就像在Web浏览器中一样。我非常喜欢移动Opera实现了这一功能,所以我只想知道是否有方法可以实现这个功能。如果有的话,该如何实现?
(我正在使用Java制作这个项目,并使用来自“WebKit”库的WebView查看网站。)
我还尝试过更改用户代理,但在静态网站上没有效果。
英文:
Is there any way to exactly implement a "desktop mode" function into my mobile app when loading website content? I'm making a android app, and I want to have a page just with static website content just like in a web browser, I really like how mobile opera implemented this feature, So I just want to know if there is a way to do that. And if there is, then How?
(I'm making this project in Java and viewing the website using WebView from the library "WebKit")
I've also tried changing the User Agent, which didn't work on a static website.
答案1
得分: 1
以下是翻译好的部分:
这个方法帮助你在 WebView 上设置桌面模式
public void setDesktopMode(WebView webView, boolean enabled) {
String newUserAgent = webView.getSettings().getUserAgentString();
if (enabled) {
newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
}
webView.getSettings().setUserAgentString(newUserAgent);
webView.getSettings().setUseWideViewPort(enabled);
webView.getSettings().setLoadWithOverviewMode(enabled);
webView.reload();
}
像这样调用它
移动模式:`setDesktopMode(webView, false);`
桌面模式:`setDesktopMode(webView, true);`
对于 Kotlin:
fun setDesktopMode(webView: WebView, enabled: Boolean) {
var newUserAgent: String? = webView.settings.userAgentString
if (enabled) {
newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
}
webView.settings.apply {
userAgentString = newUserAgent
useWideViewPort = enabled
loadWithOverviewMode = enabled
}
webView.reload()
}
英文:
This method helps you to set DesktopMode on webview
public void setDesktopMode(WebView webView,boolean enabled) {
String newUserAgent = webView.getSettings().getUserAgentString();
if (enabled) {
newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
}
webView.getSettings().setUserAgentString(newUserAgent);
webView.getSettings().setUseWideViewPort(enabled);
webView.getSettings().setLoadWithOverviewMode(enabled);
webView.reload();
}
Call it like that
Mobile mode : setDesktopMode(webView, false);
Desktop mode : setDesktopMode(webView, true);
For Kotlin:
fun setDesktopMode(webView: WebView, enabled: Boolean) {
var newUserAgent: String? = webView.settings.userAgentString
if (enabled) {
newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15"
}
webView.settings.apply {
userAgentString = newUserAgent
useWideViewPort = enabled
loadWithOverviewMode = enabled
}
webView.reload()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论