要實現Android兩端對齊的文字排版效果,我們當然可以繼承原有的TextView來實現,但一個更簡單的方式就是使用WebView,利用HTML樣式來實現。
首先定義一個String常量,我們可以將它視為一個HTML模板:
private static final String WEBVIEW_CONTENT = "<html><head></head><body style=\"text-align:justify;margin:0;\">%s</body></html>";
接下來在Layout文件中定義WebView:
<WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" />
最后在Activity中:
WebView webView = (WebView)findViewById(R.id.webview); webView.setVerticalScrollBarEnabled(false); webView.loadData(String.format(WEBVIEW_CONTENT, yourContent, "text/html", "utf-8");