java 如何在android中为TextView的一部分着色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7347617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to color part of TextView in android?
提问by Kalimah
I am trying to change the color of certain words (Search results) in a TextView
? I tried to use ANSI colors like this:
我正在尝试更改 ? 中某些单词(搜索结果)的颜色TextView
。我尝试使用这样的 ANSI 颜色:
text.setText("\u001B31;1m" + "someText");
but it did not work. How do I achieve this?
但它没有用。我如何实现这一目标?
回答by RajaReddy PolamReddy
this will help u
这将帮助你
Spannable WordtoSpan = new SpannableString("I know just how to whisper");
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 13,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(WordtoSpan);
回答by Nikolay Ivanov
You're looking for TextAppearanceSpan
and 'SpannableString'
To be more clear workflow is as follow
您正在寻找TextAppearanceSpan
和 'SpannableString' 更清晰的工作流程如下
- Create SpannableString from your source String
- Create TextAppearanceSpan and set it via call to setSpan method on SpannableString
- Call textView.setText method with SpannableString as argument
- 从源字符串创建 SpannableString
- 创建 TextAppearanceSpan 并通过调用 SpannableString 上的 setSpan 方法设置它
- 使用 SpannableString 作为参数调用 textView.setText 方法
回答by Marek
You can also use HTML like in the following example:
您还可以像以下示例一样使用 HTML:
string = "<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>";
textView.setText(Html.fromHtml(string));
Without using additional variable (one-line solution):
不使用额外变量(单行解决方案):
textView.setText(Html.fromHtml("<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>"));
It is quite easy and understandable solution :)
这是非常简单易懂的解决方案:)
回答by Paulo Miguel Almeida
I know I'm a bit late for the party but I think this is going to help future visitors out.
我知道我参加派对有点晚了,但我认为这将有助于未来的访客。
You might wanna set partial text colors on the layout XML instead of using Java code, so based on previous answers on this thread I've created a small class that does the trick.
您可能想在布局 XML 上设置部分文本颜色,而不是使用 Java 代码,因此根据此线程上的先前答案,我创建了一个小类来实现这一点。
1 - First let's create our component
1 - 首先让我们创建我们的组件
package yourpackagehere.component;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import yourpackagehere.R;
public class FontSpannableTextView extends TextView {
public FontSpannableTextView(Context context) {
super(context);
}
public FontSpannableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setColorPartialString(context, attrs);
}
public FontSpannableTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setColorPartialString(context, attrs);
}
private void setColorPartialString(Context context, AttributeSet attrs) {
if (isInEditMode()) {
return;
}
String partialText = null;
int partialTextColor = Integer.MIN_VALUE;
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontSpannableTextView);
for (int i = 0; i < a.getIndexCount(); i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.FontSpannableTextView_fontspannabletextview_partialText:
partialText = a.getString(attr);
break;
case R.styleable.FontSpannableTextView_fontspannabletextview_partialTextColor:
partialTextColor = a.getColor(attr, Color.BLACK);
break;
}
}
a.recycle();
}
if (partialText != null && partialTextColor != Integer.MIN_VALUE) {
String wholeText = getText().toString();
Spannable spannable = new SpannableString(wholeText);
spannable.setSpan(new ForegroundColorSpan(partialTextColor),
wholeText.indexOf(partialText),
wholeText.indexOf(partialText) + partialText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(spannable);
} else {
Log.e("YOURTAGHERE","You must provide both partialText and partialTextColor values");
}
}
}
2 - on attrs.xml
2 - 在 attrs.xml 上
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FontSpannableTextView">
<attr name="fontspannabletextview_partialText" format="string" />
<attr name="fontspannabletextview_partialTextColor" format="color" />
</declare-styleable>
</resources>
3 - Let's use it in our test layout
3 - 让我们在我们的测试布局中使用它
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<yourpackagehere.component.FontSpannableTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" <!-- Hello world! -->
android:layout_margin="25dp"
app:fontspannabletextview_partialText="@string/world" <!-- world! -->
app:fontspannabletextview_partialTextColor="@color/tutorial_yellow"
android:textSize="40sp"
/>
</LinearLayout>
Example:
例子: