我是Android新手,我想加载一个网址(即http://www.bhaskar.com/article/INT-sri-lanka-emergency-laws-to-end-2-years-after-war%E2%80%8E-2381626.html)。
它包含印地语数据。我正在使用loadUrl(url)
方法加载此网页,但它正在加载WebView中的方框。我必须给出印地语字体。
请向我建议如何将此网页加载到我的WebView应用程序中的步骤。我已经尝试了很长时间但没有结果。我热切期待你的回答。
任何帮助:提前致谢!
我目前的源代码是:
public class WebviewDemo extends Activity {
public static String URL = "http://www.bhaskar.com/article/INT-sri-lanka-emergency-laws-to-end-2-years-after-war%E2%80%8E-2381626.html";
WebView ArticleView;
String htmlsting=null,head=null;
Typeface typeface;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
typeface = Typeface.createFromAsset(getBaseContext().getAssets(), "DroidHindi.ttf");
//head="<head><style>@font-face {font-family: 'typeface';src: url('file:///android_asset/DroidHindi.ttf');}</style></head>";
//htmlsting="<html>"+head+"<body font-family: 'typeface'></body></html>";
// htmlsting ="<html><head><style>@font-face {font-family:'typeface';src: url('file:///android_asset/DroidHindi.ttf');}</style></head><body 'style=font-family:typeface';></body></html>";
System.out.println("***************URL is:****************"+URL);
ArticleView = (WebView) findViewById(R.id.Article_webview);
WebSettings webSettings = ArticleView.getSettings();
ArticleView.getSettings().setLoadWithOverviewMode(true);
ArticleView.getSettings().setUseWideViewPort(true);
webSettings.setJavaScriptEnabled(true);
ArticleView.getSettings().setDefaultTextEncodingName("utf-8");
try {
ArticleView.loadUrl(URL);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
*
* Closes the WebView
*/
public void cancelActivity(View v) {
ArticleView.clearCache(true);
this.finish();
}
}
答案 0 :(得分:0)
您的代码似乎正确!
确保您使用真实设备或模拟器进行应用。
您必须为模拟器/设备安装印地语字体,然后您将能够看到正确的印地文字体而不是方框。
答案 1 :(得分:0)
你需要像这样补充它
webView = (WebView) findViewById(R.id.watchClipWebView);
final WebSettings settings = webView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
final Activity activity = this;
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, failingUrl + "\n" + description,
Toast.LENGTH_SHORT).show();
}
@Override
public void onPageFinished(WebView view, String url) {
handlePageLoaded();
}
});
webView.loadUrl(sUrl);
}
private void handlePageLoaded() {
mProgressDialog.dismiss();
}
总之你错了webView.setWebViewClient + webView.setWebChromeClient
答案 2 :(得分:0)
检查以下内容:
您不需要添加字体 - 页面会自动填充字体(在Chrome DevTools中查看&gt;资源标签&gt;字体)
http://www.bhaskar.com/dainikbhaskar2010/font_family/Bhaskar_Web_Head_Test_exp.ttf http://www.bhaskar.com/dainikbhaskar2010/font_family/Bhaskar_WEB_Intro_Test_ship.ttf
确保您在清单中拥有互联网权限:
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>