[英]TextView in Android doesn't show any text after '/' character
我有一个 TextView 显示参数名称、参数值和单位。 (如“速度 5 m/s”)
由于我需要该值来更改它的颜色,因此我使用处理程序在两个字符串之间切换。 这两个字符串都注入了 html 代码,因此我可以更改颜色而无需拥有多个 TextView。
我现在的问题是,如果我的字符串中有一个破折号('/'),它后面的所有字符都不会显示。
如果我替换字符串中的斜杠,它就可以工作。 但这并不是真正的解决方案。
private void setBlinkText(){
try{
strBlinkOff = "speed <font color='#fafafa'>12</font> m/s";
strBlinkOn = "speed <font color='#212121'>12</font> m/s";
m_displayLine.setText(strBlinkOff.substring(0, strBlinkOff.indexOf('<')));
m_displayLine.append(Html.fromHtml(strBlinkOff.substring(strBlinkOff.indexOf('<'), strBlinkOff.indexOf("font>")), Html.FROM_HTML_MODE_LEGACY));
m_displayLine.append(strBlinkOff.substring(strBlinkOff.indexOf("font>") + 5));
m_blinkHandler = new Handler();
final String strFinalBlinkOn = strBlinkOn;
m_blinkHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
m_displayLine.setText(strFinalBlinkOn.substring(0, strFinalBlinkOn.indexOf('<')));
m_displayLine.append(Html.fromHtml(strFinalBlinkOn.substring(strFinalBlinkOn.indexOf('<'), strFinalBlinkOn.indexOf("font>")), Html.FROM_HTML_MODE_LEGACY));
m_displayLine.append(strFinalBlinkOn.substring(strFinalBlinkOn.indexOf("font>") + 5));
m_blinkHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
setBlinkText(p_strMessage);
}
}, 800);
}
}, 300);
} catch(Exception e)
{
Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
m_displayLine 应该显示“速度 12 m/s”,但只显示“速度 12 m/”
编辑:TextView 看起来像这样:
<TextView
android:id="@+id/ToolTextLine2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:background="@color/accentGreenLight"
android:fontFamily="@font/lordmeg09"
android:maxLength="16"
android:maxLines="1"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="#2d373c"
android:textSize="28sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/guidelineTool2" />
它可以在没有自动调整大小和较小的文本大小的情况下工作。 但是为什么会断掉呢?
看起来 maxLines 属性被忽略了,因为我在设置它之后将文本附加到我的 TextView 。 我通过设置修复它
m_displayLine2.setLines(1);
在附加我的字符串的所有部分之后再次。
尝试使用/
而不是/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.