簡體   English   中英

Android Webview無法在設備上調整大小

[英]Android webview not resize on device

我正在開發我的第一個Android應用程序。 我在Android的WebView組件中遇到了一個非常奇怪的問題。 我的網站具有響應式設計。 問題是它可以在Emulator上調整大小,但是當我在設備上運行時,Webview顯示正常設計,但沒有響應。 任何想法? 這是我的代碼:

MainActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

WebView myWebView;

@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

 myWebView = (WebView) findViewById(R.id.webview);
 WebSettings webSettings = myWebView.getSettings();
 webSettings.setJavaScriptEnabled(true); 
 myWebView.getSettings().setPluginState(PluginState.ON); 
 myWebView.loadUrl("http://apelarse.com.ar");

 myWebView.setWebViewClient(new myWebViewClient());
}

private class myWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    view.loadUrl(url);
    return false;
}

    } 

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK){
        if(myWebView.canGoBack()){
            myWebView.goBack();
            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
  } 
}

main.xml中

<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=".MainActivity">

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

我從以下站點復制了代碼: http : //samadmalik.com/converting-website-android-app/

嘗試這樣的事情:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<WebView 
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    />  

</LinearLayout>

我希望它能工作(即使乍看之下webview參數看起來有些奇怪)。

您可能需要添加以下內容:

webview.getSettings().setUseWideViewPort(true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM