Android 如何为句子中的特定单词设置文本颜色?
以下是我的回应:Android 如何为句子中的特定单词设置文本颜色?,android,android-layout,Android,Android Layout,以下是我的回应: Upto 1Feet $2 After 3Feet $0.025 在上面的响应中,我需要将绿色设置为$2和$0.025。其他文本包含黑色 这是我的xml代码 <android.support.v7.widget.CardView android:id="@+id/book_cardview_estimate_layout_driver" android:layout_
Upto 1Feet $2
After 3Feet $0.025
在上面的响应中,我需要将绿色设置为$2和$0.025。其他文本包含黑色
这是我的xml代码
<android.support.v7.widget.CardView
android:id="@+id/book_cardview_estimate_layout_driver"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/book_cardview_area_layout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"
android:layout_marginTop="30dp"
android:background="#ffffff"
android:visibility="visible"
card_view:cardElevation="8dp">
<RelativeLayout
android:id="@+id/estimate_layout_driver"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:visibility="visible">
<com.mylibrary.widgets.CustomTextView
android:id="@+id/estimate_driver_text_upto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="13dp"
android:text="Upto "
android:textColor="#000000"
android:textSize="15dp" />
<com.mylibrary.widgets.CustomTextView
android:id="@+id/estimate_driver_text_after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/estimate_driver_text_upto"
android:layout_marginLeft="10dp"
android:layout_marginTop="13dp"
android:layout_marginBottom="15dp"
android:text="After"
android:visibility="visible"
android:textColor="#000000"
android:textSize="15dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
说明:
getResources().getString(R.string.upto) = upto text,
min_feet = 1 (its comes from json response),
getResources().getString(R.string.feet) = feet,
sCurrencySymbol = $ (this comes from json response),
min_fare = 2 (some amount , comes from json response)
使用你可以改变部分句子的风格
例如:
// text1 for different color
text1 = "Upto 1Feet $";
// text 2 in different color
String text2 = min_fare;
span1 = new SpannableString(text1);
// optional if you want to change text size
span1.setSpan(new AbsoluteSizeSpan(textSize1), 0, text1.length(), SPAN_INCLUSIVE_INCLUSIVE);
// code for changing color of string 1
span1.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)), 0,
text1.length(), 0);
// do same for string two and concat both span
CharSequence finalText = TextUtils.concat(span1, span2);
txtEventType.setText(finalText);
编辑
无效设置范围(对象是什么,
int start,
int end,
int标志)
对象内容:应用什么ForegroundColorSpan/AbsoluteSizeSpan,开始:字符串开始,结束:字符串结束,标志:
将字符串设置为Html,例如
$2
和带有Html.fromHtml()的setText
比如
textview.setText(Html.fromHtml(getResources().getString(R.string.upto) + " " + min_feet +
getResources().getString(R.string.feet) +
" <font color=#00FF00>" + sCurrencySymbol + min_fare + "</font></b>"));
textview.setText(Html.fromHtml(getResources().getString(R.string.upto)+“”+min+
getResources().getString(R.string.feet)+
“+sCurrencySymbol+min_fare+”);
Spannable wordStr=new SpannableString(getResources().getString(R.string.upto)+“”+min\u-feet+getResources().getString(R.string.feet)+“”+sCurrencySymbol+min\u-fare);wordStr.setSpan(新的ForegroundColorSpan(ContextCompat.getColor(context,R.color.green)),9,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;Tv_估计_驱动程序_文本_高达.setText(wordStr);对此有疑问,我只为currencysymbol和min_票价值设置了绿色。所以我设置的起始值是9,我设置的是结束值。因为minfare包含值。所以min_票价长度与json响应不同,我找到了单行代码。得到我的答案!!!wordStr.setSpan(新的ForegroundColorSpan(ContextCompat.getColor(context,R.color.green)),10,wordStr.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);这对我有用。多谢各位@slee@Ana如果对你有用,那么请接受答案。Tv\u estimate\u driver\u text\u upto.setText(Html.fromHtml(getResources().getString(R.string.upto)+“”+min\u foots+getResources().getString(R.string.foots)+“”+sCurrencySymbol+min\u fare+“”);Tv_estimate_driver_text_after.setText(Html.fromHtml(getResources().getString(R.string.after)+“”+after_per_feet+getResources().getString(R.string.feet)+“”+sCurrencySymbol+after_per_multi+“”);
textview.setText(Html.fromHtml(getResources().getString(R.string.upto) + " " + min_feet +
getResources().getString(R.string.feet) +
" <font color=#00FF00>" + sCurrencySymbol + min_fare + "</font></b>"));