Android之同一個TextView設定不同樣式的文字,androidtextview

來源:互聯網
上載者:User

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的文字。

最後希望此文能夠對讀者有所協助。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.