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這個字最大,而且這個字水平居中顯示了)。
