我正在尝试使用自动连字功能完全证明Android中的文本。我按照here解释使用WebView实现了充分的理由。我已经在Android中阅读了几个关于自动连字的线程,但它们都没有应用于WebViews。我尝试过使用新的CSS3连字符:auto(包括-webkit-hyphens:auto),但Android WebKit还不支持它。
我发现了一篇博文here,其中提到了使用连字符JavaScript,但我不知道如何实现它(JavaScript和HTML是待办事项列表中的下一个)。由于.js文件的大小,我不想简单地使用webView.loadUrl("javascript:someFunction()");
以下是我目前使用的代码:
public class TestWebView extends Activity {
@Override
public void onCreate (Bundle savedState) {
WebView webView;
super.onCreate(savedState);
webView = new WebView(this);
setContentView(webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/WorkingExample.html");
}
}
这是HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Hyphenator.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
width:30%;
margin-left:35%;
margin-right:35%;
}
.text {
text-align:justify;
}
</style>
<script src="Hyphenator.js" type="text/javascript"></script>
<script type="text/javascript">
Hyphenator.config({
displaytogglebox : true,
minwordlength : 4
});
Hyphenator.run();
</script>
</head>
<body>
<h1>Example of using Hyphenator.js</h1>
<h2>Deutsch</h2>
<p class="hyphenate text" lang="de">Deutschsprachige Beispieltexte haben natürlicherweise längere Wortzusammensetzungen als englischsprachige. Aber auch <span lang="en">“hyphenation”</span> ist ein ziemlich langes Kompositum.</p>
<p class="hyphenate text" lang="de">Verändern Sie die Fenstergrösse um den Effekt der Silbentrennung zu sehen.</p>
<h2>English</h2>
<p class="hyphenate text" lang="en">English words are shorter in the average then german words. <span lang="de">«Silbentrennungsalgorithmus»</span> for example is quite long.</p>
<p class="hyphenate text" lang="en">Resize the window to see hyphenation in effect.</p>
<h2>Links</h2>
<p class="hyphenate text" lang="en">Not only words but also links like <a href="http://code.google.com/p/hyphenator/">http://code.google.com/p/hyphenator/</a> are processed. But in a special manner (using zero width space).</p>
</body>
</html>
与html文件一起存储的是Hyphenator.js文件。在我的计算机浏览器中打开HTML文件按预期工作,但在手机上我没有运气:
最终,我希望动态生成文本,但只是让它工作将是一个巨大的帮助。感谢。
答案 0 :(得分:0)
你WorkingExample.html
中有Hyphenator.js
和file:///android_asset/
吗?我想您忘了下载“模式”文件夹(在您需要de.js
下需要en-us.js
和file:///android_asset/patterns/
)。你可以得到他们here。
<强>更新强> 抓住您需要的一切并将其放入您的资产中:
WorkingExample.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Hyphenator.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="Hyphenator.js" type="text/javascript"></script>
<script type="text/javascript">
Hyphenator.config({
minwordlength : 4
});
</script>
</head>
<body>
<p id="text" class="hyphenate text" lang="en"></p>
</body>
</html>
MainActivity:
public class MainActivity extends Activity {
WebView mWebView;
String mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = "English words are shorter in the average then german words. <span lang=\"de\">Silbentrennungsalgorithmus</span> for example is quite long.";
mWebView = (WebView) findViewById(R.id.mWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/WorkingExample.html");
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:(function() { "
+ "document.getElementById('text').innerHTML='" + mText
+ "';" + "Hyphenator.run();" + "})()");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="@+id/mWebView"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 1 :(得分:0)
以下库支持连字符。它为您执行所有类型的文本对齐(左/右/中心/对齐)和连字符。并非所有语言都已添加,但可以根据需要添加。此库使用 NO WEBVIEWS 和 SUPPORTS SPANNABLES ,并允许 LONG TEXT 。
LIBRARY :https://github.com/bluejamesbond/TextJustify-Android
ANDROID :2.2到5.X
<强> SETUP 强>
DocumentView documentView = addDocumentView(new StringBuilder("Your long text content"), DocumentView.PLAIN_TEXT);
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.getDocumentLayoutParams().setHyphenator(new Hyphenator(HyphenPattern.PT));
documentView.getDocumentLayoutParams().setHyphenated(true);