通过TextView中的行控制对齐方式

时间:2019-09-04 07:15:53

标签: android android-studio textview

我有TextView:

        <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY TITLE\n\n
first line.................................................... \n
second line.................................................... \n
third line....................................................  \n"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/title" />

这会带来左对齐文本:

enter image description here

如何仅使文本MY TITLE的第一行居中?

UPD:

下面的代码仍不能居中MY TITLE字符串:

t.setText(Html.fromHtml("<h><center>MY TITLE</center></h>\n" +
                "<p>first line....................................................</p> </br>\n" +
                "<p>second line....................................................</p> </br>\n" +
                "<p>third line....................................................</p> </br>"));

enter image description here

1 个答案:

答案 0 :(得分:1)

U可以将其分隔为两个TextView ,也可以使用 Html.fromHtml

 t.setText(Html.fromHtml("<div style='text-align:center' >MYTITLE</center></div>\n" +
            "<p>first line....................................................</p> </br>\n" +
            "<p>second line....................................................</p> </br>\n" +
            "<p>third line....................................................</p> </br>"));