在Android WebView中加載網頁圖片,可以通過以下步驟實現:
<uses-permission android:name="android.permission.INTERNET" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://example.com"); // 替換為您要加載的網頁URL
}
}
onPageFinished()
方法來實現:import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new CustomWebViewClient());
webView.loadUrl("https://example.com"); // 替換為您要加載的網頁URL
}
private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.evaluateJavascript("javascript:window.onload = function() { " +
"var images = document.getElementsByTagName('img'); " +
"for (var i = 0; i < images.length; i++) {" +
" images[i].style.display = 'block'; " +
" images[i].style.width = '100%'; " +
" images[i].style.height = 'auto'; " +
"} " +
"}", null);
}
}
}
這樣,當網頁加載完成后,所有的圖片將自動顯示出來。如果需要進一步處理圖片,可以在JavaScript代碼中添加相應的邏輯。