Android開發:文本實現兩端對齊

關於文本兩端對齊的問題,可以參考如下:

To justify text in android I used WebView
  1. setContentView(R.layout.main); 
  2. WebView view = new WebView(this); 
  3. view.setVerticalScrollBarEnabled(false); 
  4. ((LinearLayout)findViewById(R.id.inset_web_view)).addView(view); 
  5. view.loadData(getString(R.string.hello), "text/html""utf-8"); 
and html
  1. <string name="hello"> 
  2. <![CDATA[ 
  3. <html> 
  4.  <head></head> 
  5.  <body style="text-align:justify;color:gray;background-color:black;"> 
  6.   Lorem ipsum dolor sit amet, consectetur  
  7.   
  8.  adipiscing elit. Nunc pellentesque, urna 
  9.   nec hendrerit pellentesque, risus massa 
  10.  </body> 
  11. </html> 
  12. ]]> 
  13. </string> 

I can't yet upload p_w_picpaths to prove it but "it works for me".

主要思路:使用WebView來加載,用css來實現兩端對齊。

注意:webview要設置成透明的,還有加載時使用

 myWebView.loadDataWithBaseURL("", getString(R.string.desc), "text/html", "utf-8",""); 這個方法

// myWebView.loadData(getString(R.string.desc), "text/html", "utf-8");  // 這個方法遇到一些字符中會亂碼

 
 
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章