hpi*_*que 188 formatting android text textview
是否可以在TextView中设置文本范围的颜色?
我想做一些类似于Twitter应用程序的东西,其中一部分文本是蓝色的.见下图:

Dan*_*anO 403
另一个答案非常相似,但不需要设置TextView两次的文本
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(wordtoSpan);
Run Code Online (Sandbox Code Playgroud)
mac*_*ach 80
这是一个小帮助功能.非常适合您拥有多种语言!
private void setColor(TextView view, String fulltext, String subtext, int color) {
view.setText(fulltext, TextView.BufferType.SPANNABLE);
Spannable str = (Spannable) view.getText();
int i = fulltext.indexOf(subtext);
str.setSpan(new ForegroundColorSpan(color), i, i + subtext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Run Code Online (Sandbox Code Playgroud)
Sur*_*gch 29
在尝试理解新概念时,我总能找到有用的视觉示例.
SpannableString spannableString = new SpannableString("Hello World!");
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
Run Code Online (Sandbox Code Playgroud)
SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(foregroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
Run Code Online (Sandbox Code Playgroud)
SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
Run Code Online (Sandbox Code Playgroud)
Tia*_*ago 28
如果您想要更多控制,可能需要检查TextPaint该类.以下是如何使用它:
final ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(final View textView) {
//Your onClick code here
}
@Override
public void updateDrawState(final TextPaint textPaint) {
textPaint.setColor(yourContext.getResources().getColor(R.color.orange));
textPaint.setUnderlineText(true);
}
};
Run Code Online (Sandbox Code Playgroud)
Jor*_*sys 18
设置您TextView的文本可跨越并ForegroundColorSpan为您的文本定义一个.
TextView textView = (TextView)findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(wordtoSpan);
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 12
在某些情况下可以使用的另一种方法是在采用Spannable的视图的属性中设置链接颜色.
例如,如果您的Spannable将用于TextView,您可以在XML中设置链接颜色,如下所示:
<TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorLink="@color/your_color"
</TextView>
Run Code Online (Sandbox Code Playgroud)
您也可以在代码中设置它:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setLinkTextColor(your_color);
Run Code Online (Sandbox Code Playgroud)
Aha*_*kat 12
String text = "I don't like Hasina.";
textView.setText(spannableString(text, 8, 14));
private SpannableString spannableString(String text, int start, int end) {
SpannableString spannableString = new SpannableString(text);
ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);
spannableString.setSpan(highlightSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new BackgroundColorSpan(0xFFFCFF48), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new RelativeSizeSpan(1.5f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
Run Code Online (Sandbox Code Playgroud)
输出:
使用kotlin-ktx,可以轻松实现
bindView?.oneTimePasswordTitle?.text = buildSpannedString {
append("One Time Password ")
inSpans(
ForegroundColorSpan(ContextCompat.getColor(bindView?.oneTimePasswordTitle?.context!!,R.color.colorPrimaryText))
){
append(" (As Registered Email)")
}
}
Run Code Online (Sandbox Code Playgroud)
只是添加到已接受的答案中,因为所有答案似乎都只讨论android.graphics.Color:如果我想要的颜色定义在 中怎么办res/values/colors.xml?
例如,考虑中定义的Material Design 颜色colors.xml:
<?xml version="1.0" encoding="utf-8"?>\n<resources>\n <color name="md_blue_500">#2196F3</color>\n</resources>\nRun Code Online (Sandbox Code Playgroud)\n\n(android_material_design_colours.xml是你最好的朋友)
ContextCompat.getColor(getContext(), R.color.md_blue_500)然后在您要使用的地方使用Color.BLUE,以便:
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);\nRun Code Online (Sandbox Code Playgroud)\n\n变成:
\n\nwordtoSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.md_blue_500)), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);\nRun Code Online (Sandbox Code Playgroud)\n\n我在哪里发现:
\n\n\n小智 5
有一个工厂用于创建Spannable,并避免演员,如下所示:
Spannable span = Spannable.Factory.getInstance().newSpannable("text");
Run Code Online (Sandbox Code Playgroud)
设置颜色对文本的传递字符串和颜色:
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
Run Code Online (Sandbox Code Playgroud)
通过调用以下代码在TextView/Button/EditText等上设置文本:
TextView的:
TextView txtView = (TextView)findViewById(R.id.txtView);
Run Code Online (Sandbox Code Playgroud)
获取彩色字符串:
String name = getColoredSpanned("Hiren", "#800000");
Run Code Online (Sandbox Code Playgroud)
在TextView上设置文本:
txtView.setText(Html.fromHtml(name));
Run Code Online (Sandbox Code Playgroud)
完成
这是我为此提供的 Kotlin 扩展功能
fun TextView.setColouredSpan(word: String, color: Int) {
val spannableString = SpannableString(text)
val start = text.indexOf(word)
val end = text.indexOf(word) + word.length
try {
spannableString.setSpan(ForegroundColorSpan(color), start, end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
text = spannableString
} catch (e: IndexOutOfBoundsException) {
println("'$word' was not not found in TextView text")
}
}
Run Code Online (Sandbox Code Playgroud)
像这样将文本设置为 TextView 后使用它
private val blueberry by lazy { getColor(R.color.blueberry) }
textViewTip.setColouredSpan("Warning", blueberry)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151861 次 |
| 最近记录: |