append() 后 TextView 中的文本格式不正确
Not well formed text in TextView after append()
我的 TextView 有问题。我以前没见过这个问题,Google 没有帮助。
我有 TextViews 和文本,它们在 xml 中定义,如字符串资源。
<?xml version="1.0" encoding="utf-8"?>
...
<TextView
android:id="@+id/TVName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/name"
android:textSize="@dimen/textViewTextSize"
android:textStyle="bold"
android:textColor="@color/white"
android:textAllCaps="true"
android:layout_marginEnd="@dimen/marginBetweenTextViewsEnd"
android:background="@color/gray41"
/>
...
在我的代码中,我正在为此 TextView 执行 append()
public class Page extends AppCompatActivity {
TextView TVName;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_profile);
TVName = findViewById(R.id.TVName);
TVName.append(" " + "danya");
}
}
string.xml
<resources>
<string name="name">Name:</string>
</resources>
而不是 NAME: danya
我得到了一些象形文字或者只是像 "NAME: 3ô"
这样的垃圾
首先,我使用 Retrofit 从服务器获取 append()
的文本,并认为这是来自服务器的格式错误的文本,但是当我尝试上面的代码时问题重复出现。
当我更改 setText()
上的附加时 - 一切正常。
我也曾尝试在 xml 中更改字符串资源,如 android:text="Name:"
等文本,但同样的问题。
所有文件一如既往地以 UTF-8 编码,没有手动编码。
更好的解决方案是在 string.xml
:
中加入这样的字符串
<resources>
<string name="name">Name: %s</string>
</resources>
然后,不要在 xml 中设置该字符串,只需以编程方式设置它:
TVName = findViewById(R.id.TVName);
String formattedName = String.format(getResources().getString(R.string.name), "danya");
TVName.setText(formattedName);
这里有一个很好的解释 String::format
和所有你可以用来传递不同数据格式的参数:https://dzone.com/articles/java-string-format-examples
我已经尝试删除 textAllCaps 属性并且...它有效。我认为 append 不适用于 textAllCaps。
但是我的代码需要大写,所以我的解决方案是:
String getSTVName = TVName.getText().toString();
String STVName = String.format(getSTVName, "danya");
TVName.setText(STVName);
我的 TextView 有问题。我以前没见过这个问题,Google 没有帮助。 我有 TextViews 和文本,它们在 xml 中定义,如字符串资源。
<?xml version="1.0" encoding="utf-8"?>
...
<TextView
android:id="@+id/TVName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/name"
android:textSize="@dimen/textViewTextSize"
android:textStyle="bold"
android:textColor="@color/white"
android:textAllCaps="true"
android:layout_marginEnd="@dimen/marginBetweenTextViewsEnd"
android:background="@color/gray41"
/>
...
在我的代码中,我正在为此 TextView 执行 append()
public class Page extends AppCompatActivity {
TextView TVName;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_profile);
TVName = findViewById(R.id.TVName);
TVName.append(" " + "danya");
}
}
string.xml
<resources>
<string name="name">Name:</string>
</resources>
而不是 NAME: danya
我得到了一些象形文字或者只是像 "NAME: 3ô"
首先,我使用 Retrofit 从服务器获取 append()
的文本,并认为这是来自服务器的格式错误的文本,但是当我尝试上面的代码时问题重复出现。
当我更改 setText()
上的附加时 - 一切正常。
我也曾尝试在 xml 中更改字符串资源,如 android:text="Name:"
等文本,但同样的问题。
所有文件一如既往地以 UTF-8 编码,没有手动编码。
更好的解决方案是在 string.xml
:
<resources>
<string name="name">Name: %s</string>
</resources>
然后,不要在 xml 中设置该字符串,只需以编程方式设置它:
TVName = findViewById(R.id.TVName);
String formattedName = String.format(getResources().getString(R.string.name), "danya");
TVName.setText(formattedName);
这里有一个很好的解释 String::format
和所有你可以用来传递不同数据格式的参数:https://dzone.com/articles/java-string-format-examples
我已经尝试删除 textAllCaps 属性并且...它有效。我认为 append 不适用于 textAllCaps。 但是我的代码需要大写,所以我的解决方案是:
String getSTVName = TVName.getText().toString();
String STVName = String.format(getSTVName, "danya");
TVName.setText(STVName);