Android TextView Justify Text

前端 未结 29 1172
滥情空心
滥情空心 2020-11-22 03:28

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it do

相关标签:
29条回答
  • 2020-11-22 03:58

    For html formating you don't need to call the Webkit, you could use Html.fromHtml(text) to do the job.

    Source : http://developer.android.com/guide/topics/resources/string-resource.html

    0 讨论(0)
  • 2020-11-22 04:00

    The @CommonsWare answer is correct. Android 8.0+ does support "Full Justification" (or simply "Justification", as it is sometimes ambiguously referred to).

    Android also supports "Flush Left/Right Text Alignment". See the wikipedia article on Justification for the distinction. Many people consider the concept of 'justification' to encompass full-justification as well as left/right text alignment, which is what they end up searching for when they want to do left/right text alignment. This answer explains how to achieve the left/right text alignment.

    It is possible to achieve Flush Left/Right Text Alignment (as opposed to Full Justification, as the question is asking about). To demonstrate I will be using a basic 2-column form (labels in the left column and text fields in the right column) as an example. In this example the text in the labels in the left column will be right-aligned so they appear flush up against their text fields in the right column.

    In the XML layout you can get the TextView elements themselves (the left column) to align to the right by adding the following attribute inside all of the TextViews:

    <TextView
       ...
       android:layout_gravity="center_vertical|end">
       ...
    </TextView>
    

    However, if the text wraps to multiple lines, the text would still be flush left aligned inside the TextView. Adding the following attribute makes the actual text flush right aligned (ragged left) inside the TextView:

    <TextView
       ...
       android:gravity="end">
       ...
    </TextView>
    

    So the gravity attribute specifies how to align the text inside the TextView layout_gravity specifies how to align/layout the TextView element itself.

    0 讨论(0)
  • 2020-11-22 04:00

    TextView in Android O offers full justification (new typographic alignment) itself.

    You just need to do this:

    Kotlin

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        textView.justificationMode = JUSTIFICATION_MODE_INTER_WORD
    }
    

    Java

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        textView.setJustificationMode(JUSTIFICATION_MODE_INTER_WORD);
    }
    

    default is JUSTIFICATION_MODE_NONE.

    0 讨论(0)
  • 2020-11-22 04:00

    XML Layout: declare WebView instead of TextView

    <WebView
     android:id="@+id/textContent"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" />
    

    Java code: set text data to WebView

    WebView view = (WebView) findViewById(R.id.textContent);
    String text;
    text = "<html><body><p align=\"justify\">";
    text+= "This is the text will be justified when displayed!!!";
    text+= "</p></body></html>";
    view.loadData(text, "text/html", "utf-8");
    

    This may Solve your problem. Its Fully worked for me.

    0 讨论(0)
  • 2020-11-22 04:03

    I do not believe Android supports full justification.

    UPDATE 2018-01-01: Android 8.0+ supports justification modes with TextView.

    0 讨论(0)
  • 2020-11-22 04:03

    Try this solution in the below link just create that class in project folder and use it. it works fine for me :)

    Justify text in an Android app using a WebView but presenting a TextView-like interface?

    0 讨论(0)
提交回复
热议问题