TextView的转义字符显示错误

时间:2019-05-29 10:13:04

标签: android textview

我有一条随机消息,其中包含转义字符。我发现TextView的转义字符"\t"显示错误的空格。

我的示例项目的Activity具有仅包含一个TextView的布局。

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="@string/text_hello"
        android:maxLines="5"
        />
</FrameLayout>

我在文件strings.xml中定义了文本

<string name="text_hello">Click Change: 13894\t18258\nClick Change: 1604\t2190\nClick Change: 9250\t11962</string>

相应的结果是

enter image description here

我更改如下文字

<string name="text_hello">ABC: 13894\t18258\nDEF: 1604\t2190\nXYZ: 9250\t11962</string>

相应的结果将改变 enter image description here

作为示例,TextView显示错误的空格。谁能解决此问题?

1 个答案:

答案 0 :(得分:2)

您可以使用

\u0020

&#032;

代替string.xml文件中的\t