2013-01-25 31 views
1

我试图在Android上学习web视图。我有几个问题。Android webview。在没有滚动条的屏幕上拟合HTML

  1. 我有一个PHP页面,其中有2-3小段落和其随机产生 映像。我如何确保整个HTML页面 完全适合移动设备的屏幕(使滚动条 未被看到)?如果在某些情况下不可能,我如何才能使图像变得更小以适应每个设备? 。

  2. 第二个问题是假设我在该HTML页面上有一个链接。我想要 ,当我点击这个链接(属于不同的域名)时,它在 开启了默认浏览器而不是android应用程序。 ( 怎么能这样做)。

    package com.example.try2;

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.view.Window; 
    import android.view.WindowManager; 
    import android.webkit.WebSettings; 
    import android.webkit.WebView; 
    
    public class MainActivity extends Activity { 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         //Full screen 
         requestWindowFeature(Window.FEATURE_NO_TITLE); 
         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
    
         WebView myWebView = (WebView) findViewById(R.id.webview); 
         myWebView.setVerticalScrollBarEnabled(false); 
         myWebView.setHorizontalScrollBarEnabled(false); 
         myWebView.getSettings().setLoadWithOverviewMode(true); 
         myWebView.getSettings().setUseWideViewPort(true); 
         WebSettings webSettings = myWebView.getSettings(); 
         webSettings.setJavaScriptEnabled(true); 
         myWebView.loadUrl("http://www.example.com/mobile/index.php"); 
    
        } 
    
        @Override 
        public boolean onCreateOptionsMenu(Menu menu) { 
         // Inflate the menu; this adds items to the action bar if it is present. 
         getMenuInflater().inflate(R.menu.activity_main, menu); 
         return true; 
        } 
    
    } 
    

回答

0

,使屏幕上的图像贴合,只需检查窗口的宽度,它与您的网页上的JavaScript图像的宽度进行比较。如果图像的宽度大于屏幕的宽度,请将图像的宽度设置为窗口的宽度。

查看jQuery的.width()函数。您可以使用$(window).width()$('#myimageid").width()

至于在WebView中打开链接,默认行为是使用用户的默认浏览器(不是您的WebView)打开它。如果要覆盖此行为,请WebViewClient#shouldOverrideUrl(WebView view, String url)

0

回答您的问题:

  1. 您可能实际上要做到这一点的HTML/JS代码。钩住resize事件并根据该设备上可用的像素数设置图片大小。这里是jQuery的一个例子,其中MYIMAGE是图像的ID和会将myText是在页面的顶部包含文本的div ID:

    $(window).resize(function() { 
    
        // Compute available height for image 
        // by subtracting text div height from screen height 
        var availableImageWidth = screen.width; 
        var availableImageHeight = screen.height - $('myText').height(); 
    
        // Get image width and height 
        var imageWidth = $("#myImage").width(); 
        var imageHeight = $("#myImage").height(); 
    
        // Compute scale that maintains aspect ratio and fits image within screen bounds 
        var scale = Math.min(availableImageWidth/imageWidth, availableImageHeight/imageHeight); 
    
        // Set new image dimensions 
        $("#myImage").width(imageWidth * scale); 
        $("#myImage").height(imageHeight * scale); 
    } 
    
  2. 您可以通过重写的shouldOverrideUrlLoading方法做到这一点WebViewClient类别:

    myWebView.setWebViewClient(new WebViewClient() 
    { 
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {   
         // Redirect HTTP and HTTPS urls to the external browser 
         if (url != null && URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) 
         { 
          view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
          return true; 
         } 
    
         return false; 
        } 
    }