Android之同一个TextView设置不同样式的文字

需求分析:

很多时候,我们需要在视图中显示不同样式的文字,但是为了减少viewgroup层级,不想新增很多个TextView控件来实现不同样式的文字。

那么有没有一种方式能够在同一个TextView控件中实现多种自定义的样式的文字呢?

答案是肯定的,下面就让我们来做一个此问题的实践实验。

实践过程:

首先我们在布局xml文件中定义了三个TextView控件,它们的定义如下:

                    <TextView
                        android:id="@+id/annualized_Rate_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dip"
                        android:text="10.98%"
                        android:textColor="#e61300"
                        android:textSize="30sp" />

                    <TextView
                        android:id="@+id/due_time_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="bottom"
                        android:text="12个月"
                        android:textColor="#aaaaaa"
                        android:textSize="20sp" />

                    <TextView
                        android:id="@+id/total_sum_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="bottom"
                        android:text="10万元"
                        android:textColor="#aaaaaa"
                        android:textSize="20sp" />

接着,我们在java代码中去通过使用SpannableString这样一个关键类来实现我们的需求:

				String rateContent = t.getAnnualizedRateOfReturn() + "%";
				int lenRate = rateContent.length();
				SpannableString rate = new SpannableString(rateContent);
				rate.setSpan(new TextAppearanceSpan(getActivity(), R.style.item_rate_text_style1), 0, lenRate-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				rate.setSpan(new TextAppearanceSpan(getActivity(), R.style.item_rate_text_style2), lenRate-1, lenRate, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

				String monthsContent  = String.valueOf(t.getMonths()) + "个月";
				int lenMonths = monthsContent.length();
				SpannableString months = new SpannableString(monthsContent);
				months.setSpan(new TextAppearanceSpan(getActivity(), R.style.item_month_sum_text_style1), 0, lenMonths-2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				months.setSpan(new TextAppearanceSpan(getActivity(), R.style.item_month_sum_text_style2), lenMonths-2, lenMonths, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

				String sumContent  = String.valueOf(t.getSum()) + "万元";
				int lenSum = sumContent.length();
				SpannableString sum = new SpannableString(sumContent);
				sum.setSpan(new TextAppearanceSpan(getActivity(), R.style.item_month_sum_text_style1), 0, lenSum-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				sum.setSpan(new TextAppearanceSpan(getActivity(), R.style.item_month_sum_text_style2), lenSum-1, lenSum, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

				vh.setText(R.id.name_text, t.getName() + "(" + t.getDate()
						+ ")").setStyledText(R.id.annualized_Rate_text,rate)
						.setStyledText(R.id.due_time_text, months)
						.setStyledText(R.id.total_sum_text, sum);

这里面的setStyledText方法实际上是封装了,TextView控件的setText方法,Span那边了String是CharSequence整个类的子类,因此可以作为setText方法的参数。

这里面使用了四个style,那我们的style在styles.xml文件当中定义,定义如下:

    <style name="item_rate_text_style1">
        <item name="android:textSize">30sp</item>
    </style>

    <style name="item_rate_text_style2">
        <item name="android:textSize">17sp</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="item_month_sum_text_style1">
        <item name="android:textSize">22sp</item>
        <item name="android:textColor">@color/black</item>
    </style>

    <style name="item_month_sum_text_style2">
        <item name="android:textSize">15sp</item>
    </style>

最终效果,如下图:

我们可以看到,在同一个TextView中,有两种不同style的文字。

最后希望此文能够对读者有所帮助。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 11:31:00

Android之同一个TextView设置不同样式的文字的相关文章

Android项目实战(十四):TextView显示html样式的文字

原文:Android项目实战(十四):TextView显示html样式的文字 项目需求: TextView显示一段文字,格式为:白雪公主(姓名,字数不确定)向您发来了2(消息个数,不确定)条消息 这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色. 一开始我想的是用(转) SpannableString与SpannableStringBuilder来实现,因为它可以实现一段文字显示不同的颜色 但是貌似它只能固定哪些位置的文字显示什么样式,于是乎放弃. 然后就想到了用 Html

android同一个TextView设置不同颜色字体

1 SpannableStringBuilder style = new SpannableStringBuilder(str); 2 style.setSpan( 3 new ForegroundColorSpan(getResources().getColor( 4 R.color.tab_sel_color)), 0, t1.length() - 1, 5 Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 6 style.setSpan( 7 new Foregro

Android 基础一 TextView,Style样式,Activity 传值,选择CheckBox 显示密码

1.修改TextView字体 mTextView = (TextView) findViewById(R.id.textview1); mTextView.setText("I am here"); Resources resources = getBaseContext().getResources(); Drawable myDrawable = resources.getDrawable(R.drawable.Drawable1); mTextView.setBackground

同一个TextView设置不同的颜色和大小

//strategy1是一个TextView SpannableStringBuilder builder1 = new SpannableStringBuilder(strategy1.getText().toString()); //设置前景色为蓝色 ForegroundColorSpan blue=new ForegroundColorSpan(Color.BLUE); //改变第0-3个字体颜色为蓝色 builder1.setSpan(blue,0,3, Spanned.SPAN_EXC

textView设置按下和焦点改变时让字体颜色发生变化

在res/color/text_color_selector.xml这个下编写: <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color

Android TextView设置个别字体样式

TextView进一步深化: Textview 可以对其文字进行格式化. 通过查询资料,了解到格式化文字的方式主要分为两大类: 第一类:HTML标签格式化文字 代码比较简单,如下: import android.app.Activity; import android.os.Bundle; import android.text.Html; import android.widget.TextView; public class AndroidFronColorTest extends Acti

【Android】 TextView设置个别字体样式

1 SpannableString msp = new SpannableString("测试"+XM+"更换当前号码将从手机发送一条普通短信进行验证"); 2 msp.setSpan(new ForegroundColorSpan(Color.BLUE), 2, XM.length()+2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 印象中的TextView: TextView 就是用于显示文本的控件,可以在布局文件中通过 androi

android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色

实现下划线及颜色设置: public class AtActivity extends Activity { LinearLayout ll; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); l

android selector(如对TextView点击样式改变)

selector 1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制. 从API来说: A controller for the selection of SelectableChannel objects. Selectable channels can be registered with a selector and get a SelectionKey that represents the registration. The keys are also add