Java 如何在android中为TextView的一部分着色?
我正在尝试更改Java 如何在android中为TextView的一部分着色?,java,android,android-layout,Java,Android,Android Layout,我正在尝试更改文本视图中某些单词(搜索结果)的颜色?我尝试使用ANSI颜色,如下所示: text.setText("\u001B31;1m" + "someText"); 但它没有起作用。如何实现这一点?您正在寻找文本外观span和“SpannableString” 更清楚的是,工作流程如下 从源字符串创建SpannableString 创建TextAppearanceSpan并通过调用SpannableString上的setSpan方法进行设置 以SpannableString作为参数调用t
文本视图中某些单词(搜索结果)的颜色?我尝试使用ANSI颜色,如下所示:
text.setText("\u001B31;1m" + "someText");
但它没有起作用。如何实现这一点?您正在寻找文本外观span
和“SpannableString”
更清楚的是,工作流程如下
从源字符串创建SpannableString
创建TextAppearanceSpan并通过调用SpannableString上的setSpan方法进行设置
以SpannableString作为参数调用textView.setText方法
这将帮助你
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);
您也可以像以下示例中那样使用HTML:
string = "<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>";
textView.setText(Html.fromHtml(string));
string=“这是我的文本”+“另一个文本”;
setText(Html.fromHtml(string));
不使用其他变量(单行解决方案):
textView.setText(Html.fromHtml(“这是我的文本”+“另一个文本”));
这是一个简单易懂的解决方案:)我知道我参加聚会有点晚了,但我认为这将有助于未来的游客
您可能希望在布局XML上设置部分文本颜色,而不是使用Java代码,因此根据前面关于此线程的回答,我创建了一个小类来实现此功能
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");
}
}
}
<?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>
打包yourpackagehere.component;
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.graphics.Color;
导入android.text.Spannable;
导入android.text.SpannableString;
导入android.text.style.ForegroundColorSpan;
导入android.util.AttributeSet;
在此处导入您的包。R;
公共类FontSpannableTextView扩展了TextView{
公共FontSpannableTextView(上下文){
超级(上下文);
}
public FontSpannableTextView(上下文上下文、属性集属性){
超级(上下文,attrs);
setColorPartialString(上下文,属性);
}
public FontSpannableTextView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
setColorPartialString(上下文,属性);
}
私有void setColorPartialString(上下文上下文,属性集属性){
if(isInEditMode()){
返回;
}
字符串partialText=null;
int partialTextColor=Integer.MIN\u值;
如果(属性!=null){
TypedArray a=context.actainStyledAttributes(attrs,R.styleable.FontSpannableTextView);
对于(int i=0;i
2-on 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-让我们在测试布局中使用它
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");
}
}
}
<?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>
app:fontspannabletextview_partialTextColor=“@color/tutorial_yellow”
android:textSize=“40sp”
/>
例如:
5,13是字符的长度。你是说开始和结束索引?是的。i、 e要为文本的哪个部分着色。彩色文本视图的起点和终点。请参见:Html.fromHtml(字符串)现在不推荐使用。请更新您的答案。这是一个优雅的解决方案。我已经在安卓上工作了十年,但即使在2020年,这似乎也是安卓中TextView
的一个主要问题。