2016-12-24 12 views
1

私はTextViewのテキストを正当化したいと思いましたが、TextViewでそれを行う方法が見つかりませんでしたので、WebViewを作成しました。AndroidのWebViewでテキストを正しく正当化するにはどうすればよいですか?

WebView上のテキストを設定するコードは以下の通りです:

WebView webview = (WebView) view.findViewById(R.id.webview); 
webview.loadData(getString(R.string.webview), "text/html; charset=utf-8", "utf-8"); 
webview.setBackgroundColor(Color.TRANSPARENT); 

そして、それがうまく機能(私はstyle="text-align:justify;bodyタグを作成しているため)、テキストはショーが正当化されます。

問題は、テキストをWebViewにロードしたときに、テキストを充電するのに数秒かかることです。したがって、テキストが表示される前に残りのレイアウトが表示されているので、奇妙な視覚効果があります。

WebViewが完全にロードされるまで待つことを試みましたが(How to listen for a WebView finishing loading a URL?)、テキストは表示されません。ここで私は一瞬で持っているコードです:

webview.setWebViewClient(new WebViewClient() { 

    public void onPageFinished(WebView view, String url) { 
     webview.loadData(getString(R.string.webview), "text/html; charset=utf-8", "utf-8"); 
     webview.setBackgroundColor(Color.TRANSPARENT); 
    } 
}); 

だから、どのように私は、レイアウトの残りの部分と同時に正当化したテキストを表示することができますか?

ありがとうございます!

+1

https://github.com/programingjd/justified – CommonsWare

+0

重複する質問:http://stackoverflow.com/questions/1292575/android-textview-justify-text – tahaDev

+0

@tahaDev私はそれが重複しているとは思わないその質問。私はテキストを正当化する方法を知っています。私は具体的な問題について尋ねています。 –

答えて

0

これは絵です:enter image description here

これは、私はそれが

void initVebView(WebView wvContent_Detail, String body) { 
     String HTML1 = "<html><head><style type=\"text/css\">@font-face  {font-family: MyFont;src: url(\"file:///android_asset/%s\")}body {font-family: MyFont;font-size: medium;text-align: justify; line-height: %spx;}</style></head><body dir='rtl'>"; 
    String HTML2 = "</body></html>"; 
    String HTML3 = "<span style=\"font-weight:bold\">%s<br/><br/>%s</span>"; 

    String HTML4 = "<style>img{display: inline;height: auto;max-width: 100%;</style>"; 

    String str = String.format(HTML1, "IRANSansMobile_UltraLight.ttf", 25); 


    String content = body.replace("text-align:", ""); 
    content = content.replace("font-family:", ""); 
    content = content.replace("line-height:", ""); 
    content = content.replace("dir=", ""); 
    content = content.replace("width=", "width=\"100%;\""); 

    String myHtmlString = str + content + HTML2; 

    wvContent_Detail.loadDataWithBaseURL(null, HTML4 + myHtmlString, "text/html", "UTF-8", null); 

    WebSettings webSettings = wvContent_Detail.getSettings(); 
    webSettings.setDefaultFontSize(20); 
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); 
} 

これはXMLで読み込むためにそんなに取るdoesntの 私のプロジェクトで使用されるコードです:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context="ir.tenthwindow.BaseModule.ui.FragmentVideo"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <WebView 
      android:id="@+id/webview_product_details" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" /> 
    </ScrollView> 
    </FrameLayout> 

あなたのフォントを代わりに使用することができます。 これが役に立ったと思います。

関連する問題