Android - 實現兩端對齊的文字排版


要實現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");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM