[英]Xamarin color part of a TextView
I want my TextView
in my ViewSwitcher
to switch colors between green and white every new line. 我希望我的
ViewSwitcher
TextView
在每一行中在绿色和白色之间切换颜色。 I figure out how to change the color, but only of the entire TextView
with the method: 我想出了如何改变颜色的方法,但是只改变了整个
TextView
的颜色:
SetTextColor()
You would need to use TextFormatted property of the TextView. 您将需要使用TextView的TextFormatted属性。 Some sample you can find on Xamarin forums: https://forums.xamarin.com/discussion/9403/setting-a-textview-to-a-spannable-string
您可以在Xamarin论坛上找到一些示例: https : //forums.xamarin.com/discussion/9403/setting-a-textview-to-a-spannable-string
I want my TextView in my ViewSwitcher to switch colors between green and white every new line.
我希望我的ViewSwitcher中的TextView在每一行中在绿色和白色之间切换颜色。 I figure out how to change the color, but only of the entire TextView
我想出了如何更改颜色,但仅更改整个TextView的颜色
You can use TextFormatted
together with Android.Text.SpannableString
to set the color: 您可以将
TextFormatted
与Android.Text.SpannableString
一起使用来设置颜色:
int lineCount = tvResult.LineCount;
SpannableString spannableText =new SpannableString(tvResult.Text);
for (int i = 0; i < lineCount; i++)
{
int start=tvResult.Layout.GetLineStart(i);
int end = tvResult.Layout.GetLineEnd(i);
if (i % 2 == 1)
{
spannableText.SetSpan(new ForegroundColorSpan(Android.Graphics.Color.Yellow), start, end, SpanTypes.Composing);
}
else
{
spannableText.SetSpan(new ForegroundColorSpan(Android.Graphics.Color.Green), start, end,SpanTypes.Composing);
}
}
tvResult.TextFormatted = spannableText;
Notes: tvResult is just a TextView control. 注意:tvResult只是一个TextView控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.