Android TextView SpannableString多個字體大小不一致的情況下,設置字體垂直居中顯示

1、我的TextView。

<TextView

? ? ? ? ? ? ? ? ? ? android:id="@+id/tev_title"

? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? ? ? ? ? android:layout_height="@dimen/dp_50"

? ? ? ? ? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? ? ? ? ? android:text="@string/create_user"

? ? ? ? ? ? ? ? ? ? android:textColor="@color/color_FFFFFFFF"

? ? ? ? ? ? ? ? ? ? android:textSize="@dimen/sp_17" />


2、res下的values文件夾下的strings.xml文件的資源字符串。

<string name="created_b">創(chuàng)建了</string>

? ? <string name="users_b">個用戶</string>


3、TextView Style 管理類。

package com.phone.common_library.manager;

import android.content.Context;

import android.graphics.Typeface;

import android.text.Spannable;

import android.text.SpannableString;

import android.text.style.AbsoluteSizeSpan;

import android.text.style.StyleSpan;

import android.widget.TextView;

import com.phone.common_library.spannable.VerticalAlignTextSpan;

public class TextViewStyleManager {

? ? private static final String TAG = TextViewStyleManager.class.getSimpleName();

? ? public static void setTextViewStyle(TextView textView,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String data,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int start) {

? ? ? ? Spannable spannable = new SpannableString(data);

? ? ? ? // 粗體

? ? ? ? spannable.setSpan(new StyleSpan(Typeface.BOLD), start, data.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

? ? ? ? // 顯示

? ? ? ? textView.setText(spannable);

? ? }

? ? public static void setTextViewStyle(Context context,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TextView textView,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String data,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int start,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int end,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? float size) {

? ? ? ? Spannable spannable = new SpannableString(data);

? ? ? ? // 粗體

? ? ? ? spannable.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

? ? ? ? spannable.setSpan(new AbsoluteSizeSpan(ScreenManager.spToPx(context, size)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

? ? ? ? // 顯示

? ? ? ? textView.setText(spannable);

? ? }

? ? public static void setTextViewStyleVerticalCenter(Context context,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TextView textView,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String data,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int start,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int end,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? float size) {

? ? ? ? Spannable spannable = new SpannableString(data);

? ? ? ? // 粗體

? ? ? ? spannable.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

? ? ? ? spannable.setSpan(new AbsoluteSizeSpan(ScreenManager.spToPx(context, size)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

? ? ? ? //SpannableString字體大小不一致垂直居中顯示

? ? ? ? spannable.setSpan(new VerticalAlignTextSpan(context, size), start, end, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);

? ? ? ? // 顯示

? ? ? ? textView.setText(spannable);

? ? }

}


4、在Activity中設置SpannableString多個字體大小不一致的情況下,設置字體垂直居中顯示。

TextViewStyleManager.setTextViewStyleVerticalCenter(this,

? ? ? ? ? ? ? ? ? ? tevTitle, getResources().getString(R.string.created_b)

? ? ? ? ? ? ? ? ? ? ? ? ? ? + 3

? ? ? ? ? ? ? ? ? ? ? ? ? ? + getResources().getString(R.string.users_b),

? ? ? ? ? ? ? ? ? ? getResources().getString(R.string.created_b).length(),

? ? ? ? ? ? ? ? ? ? getResources().getString(R.string.created_b).length() + 1,

? ? ? ? ? ? ? ? ? ? 28);


5、效果如下(見下圖一,圖中紅圈處的3這個字最大,而且這個字水平居中顯示了)。


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容