【问题标题】:Android Webview - Loads the mobile version even on a tabletAndroid Webview - 即使在平板电脑上也能加载移动版本
【发布时间】:2016-08-29 15:13:44
【问题描述】:

我在 Android 中使用 Web 视图时遇到了问题。我正在尝试使用 Web 视图制作一个简单的应用程序,这个应用程序只需要在 Web 视图中加载一个网站。在手机等移动设备上,它可以完美运行,但是当我尝试在 WebView 中从平板电脑(例如 Nexus 10)打开应用程序时,它会加载网站的移动版本,而不是平板电脑版本。我搜索并没有找到这个问题的答案。我还检查了网站上的元标记,但它似乎在那里,这不是问题。

这是我的 .java 文件:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    private static WebView view;


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

        getActionBar().setIcon(R.drawable.icon);

        String url = "http://hoinarim.ro/";

        view = (WebView) this.findViewById(R.id.webView);

        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.getSettings().setJavaScriptEnabled(true);

        //improve webview
        view.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        view.getSettings().setAppCacheEnabled(true);
        view.getSettings().setDomStorageEnabled(true);
        view.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        view.getSettings().setUseWideViewPort(false);
        view.getSettings().setLoadWithOverviewMode(true);

        view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        view.getSettings().setSavePassword(true);
        view.getSettings().setSaveFormData(true);
        view.getSettings().setEnableSmoothTransition(true);

        view.getSettings().setDatabaseEnabled(true);
        view.getSettings().setUseWideViewPort(true);
        view.setWebViewClient(new WebViewClient());
        view.loadUrl(url);
        view.setHorizontalScrollBarEnabled(false);

        view.setOnTouchListener(new View.OnTouchListener() {
            public float m_downX;

            public boolean onTouch(View v, MotionEvent event) {

                if (event.getPointerCount() > 1) {
                    //Multi touch detected
                    return true;
                }

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    {
                        // save the x
                        m_downX = event.getX();
                    }
                    break;

                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP: {
                        // set x so that it doesn't move
                        event.setLocation(m_downX, event.getY());
                    }
                    break;

                }

                return false;
            }
        });
    }


    @Override
    public void onBackPressed() {
        if (view.canGoBack()) {
            view.goBack();
        }
        else
        {
            super.onBackPressed();
        }
    }
}

这是我的 .xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="hoinarimapp.hoinarim.MainActivity">

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

知道如何进行这项工作吗?真是令人沮丧。

【问题讨论】:

    标签: android xml android-layout mobile webview


    【解决方案1】:

    我不知道这是否会有所帮助,但这会加载网站的桌面版本。

    String userAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
    webview.getSettings().setUserAgentString(userAgent);
    

    无论如何我希望这会有所帮助:D

    【讨论】:

    • 谢谢约翰。这实际上非常有效。我尝试了不同的用户代理字符串,但没有成功,但这个就像一个魅力。
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多