【问题标题】:Android webview display mobile page differently compared to a standard browser与标准浏览器相比,Android webview 显示移动页面的方式不同
【发布时间】:2014-02-19 11:25:04
【问题描述】:

我编写了一个简单的 Android 应用程序来在 WebView 中显示维基百科移动页面:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView myWebView = (WebView) findViewById(R.id.webView);
    myWebView.setWebViewClient(new WebViewClient(){
       // @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
          view.loadUrl(url);
          return true;
        }
    });

    myWebView.loadUrl("http://en.m.wikipedia.org/");
}

正如您在我的应用程序中看到的,来自维基百科移动页面的侧边菜单占据了整个屏幕宽度。在 Chrome 中,它可以正确显示,因此可以点按以将菜单收起。

我该如何解决这个问题?谢谢。

上面的截图是用这个布局完成的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" />

我随后尝试了线性布局,结果相同:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

【问题讨论】:

  • 发布你的 webview 的 layout.xml。最有可能的问题应该在那个。我相信 Wikipedia 移动页面应该正确处理禁用 javascript 的浏览器。
  • 感谢 Sundeep。如上所述添加xml。
  • 你选择RelativeLayout有什么原因吗?如果这适用于 LinearLayout,您是否尝试过?
  • 我刚刚在浏览器中禁用了 Javascript,并看到 http://en.m.wikipedia.org/ 重定向到 http://en.m.wikipedia.org/wiki/Main_Page。如果我单击堆栈图标,则会加载 url http://en.m.wikipedia.org/wiki/Special:MobileMenu,我会看到您在屏幕截图上看到的内容。尝试显式加载http://en.m.wikipedia.org/wiki/Main_Page。如果不起作用,请尝试在嵌入式浏览器中启用 Javascript
  • 启用 JavaScript 成功了。谢谢。

标签: android webview


【解决方案1】:

Enable Javascript for the embedded browser.

我刚刚在浏览器中禁用了 Javascript 并看到了 http://en.m.wikipedia.org/ 重定向到 http://en.m.wikipedia.org/wiki/Main_Page

如果我单击堆栈图标,则会加载 url http://en.m.wikipedia.org/wiki/Special:MobileMenu,我会看到您在屏幕截图上看到的内容。

尝试显式加载http://en.m.wikipedia.org/wiki/Main_Page。我希望 wikipedia 移动页面能够处理一个没有 javascript 的浏览器,而且它似乎在我的 PC 上处理得很好。

【讨论】:

  • 只是添加,不需要显式 URL。这是 Javascript 启用修复它。
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 2012-09-07
  • 1970-01-01
相关资源
最近更新 更多