您的位置:首页 > 移动开发 > Android开发

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

2015-07-22 23:48 661 查看
需求分析:

很多时候,我们需要在视图中显示不同样式的文字,但是为了减少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的文字。

最后希望此文能够对读者有所帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: