【发布时间】: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