根据左侧空格在多个TextView中拆分文本

时间:2017-04-21 10:38:41

标签: android android-layout textview android-view

如何在两个TextView之间拆分字符串文本(蓝色),以便它在一个TextView中启动,并在另一个文本框中继续当没有剩余空间时两个TextView的最大宽度不同

一个例子是填写表单。黑色文本是静态的(两个标签)。

The result

另一个approche可能只有一个TextView (对于蓝色文本)左边和右边有填充但仅适用于第一行。 对于每个填充,填充大小将等于标签宽度。

1 个答案:

答案 0 :(得分:2)

你应该以编程方式进行

    int textSize = 16;

    textView2.setTextSize(textSize);
    textView1.setTextSize(textSize);

    final float scale = getResources().getDisplayMetrics().density;
    int dpWidthInPx  = (int) (100 * scale);

    int countTv1Chars = dpWidthInPx / textSize;
    String tv1String = string.substring(0, countTv1Chars);
    String tv2String = string.substring(countTv1Chars, string.length() - 1);
    textView1.setText(tv1String);
    textView2.setText(tv2String);

in xml

    <TextView

    android:id="@+id/tv1"
    android:lines="1"
    android:layout_width="100dp"
    android:layout_height="wrap_content" />

<TextView

    android:id="@+id/tv2"
    android:lines="1"
    android:layout_width="100dp"
    android:layout_height="wrap_content" />