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

时间:2017-11-09 05:50:04

标签: android android-layout

这是我的回答:

<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
<input type="file" name="file4">

在上面的回复中,我需要设置2美元和0.025美元的绿色。其他文字包含黑色。

这是我的xml代码,

     Upto 1Feet $2
     After 3Feet $0.025

这是java Code,

          <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>

说明:

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

2 个答案:

答案 0 :(得分:2)

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

示例:

float a[] = float[](3.4, 4.2, 5.0, 5.2, 1.1);
float b[] = { 3.4, 4.2, 5.0, 5.2, 1.1 };
float c[] = a; // c is explicitly size 5
float d[5] = b; // means the same thing

修改

  

void setSpan(对象是什么,                   int start,                   int end,                   int flags)

     

对象:应用什么ForegroundColorSpan / AbsoluteSizeSpan,start:字符串的开头,结束:字符串的结尾,标志:Flag of Spanned

答案 1 :(得分:0)

将您的字符串设为Html,例如

<b><font color=#FF0000>$2</font></b>和setText与Html.fromHtml()

,例如

    textview.setText(Html.fromHtml(getResources().getString(R.string.upto) + " " + min_feet +
     getResources().getString(R.string.feet) + 
" <font color=#00FF00>" + sCurrencySymbol + min_fare + "</font></b>"));