Android之同一個TextView設定不同樣式的文字,androidtextview
需求分析:
很多時候,我們需要在視圖中顯示不同樣式的文字,但是為了減少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的文字。
最後希望此文能夠對讀者有所協助。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。