调整Android多行TextView的大小

Wil*_*iam 10 android sizing textview android-ui android-layout

我有一个活动,我想显示一个占据应用程序前四分之一的标题,并显示一组动态文本.在某些情况下,它将全部放在一条线上,我希望字体大小可以调整大小,因此它很大且易于阅读.在某些情况下,它将在多行(通常小于4)并且需要更小.

复杂的是,如果我在setText中明确设置换行符,它有时会插入一个我不想要的换行符,即:

"My first line of text\nMy Second line of text"
Might get turned into (it's right adjusted in this version):
"My first line of
             text
   My second line
          of text"

我不确定如何保持TextView不要试图插入换行符或让它只是处理格式化整个事情.如果我将一些文本设置为一个非常长的单个单词,它将在某个选定点的中间插入换行符.有时我只是随机得到最后几行文字.

我是否应该明确地将字体大小设置为非常小以使其正确布局然后手动调整大小?我应该使用"最佳实践"吗?如果我将字体大小设置为一个相当大的字体,那么它的工作方式很有用,但要弄清楚这样做的"正确"方法会很好.而且我宁愿不必手动添加4个单行TextView并自己格式化.

我正在使用的布局xml是:

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="10dip"
        android:layout_marginRight="8dip"
        android:singleLine="false"
        android:ellipsize="marque"
        android:gravity="right|bottom"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="30sp"
/>
Run Code Online (Sandbox Code Playgroud)

Aru*_*asu 0

你能行的。将新行字符附加到字符串值。然后将此字符串值设置为 TextView 的文本属性。

String temp = "ASDF \n"+"EFG \n"+"HIJ";
TextView tv=//find it from the context
tv.setText(temp);
Run Code Online (Sandbox Code Playgroud)