gpt4 book ai didi

android - 如何为句子中的特定单词设置文本颜色?

转载 作者:搜寻专家 更新时间:2023-11-01 09:31:10 24 4
gpt4 key购买 nike

这是我的回应:

     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>

这里是java代码,

    Tv_estimate_driver_text_upto.setText(getResources().getString(R.string.upto) + " " + min_feet + getResources().getString(R.string.feet) + " " + sCurrencySymbol + min_fare);


Tv_estimate_driver_text_after.setText(getResources().getString(R.string.after) + " " + after_per_feet + getResources().getString(R.string.feet) + " " + sCurrencySymbol + after_per_multi);

解释:

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)

最佳答案

使用 Spans你可以改变部分句子的风格。

示例:

// 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);

编辑

void setSpan (Object what, int start, int end, int flags)

Object what : What to apply ForegroundColorSpan / AbsoluteSizeSpan , start : start of string,end : end of string , flags : Flag of Spanned

关于android - 如何为句子中的特定单词设置文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47194740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com