android:layout_
android:layout_
android:textColor="#FF0000"
android:textSize="18sp"/>
TextView textView = new TextView(this);
textView.setTextColor(Color.RED);
textView.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, getResources().getDisplayMetrics()));
// textView.setText(Html.fromHtml("TextView")); // 该方式不支持设置字体大小
String text = "年收益率:5.88%";
SpannableString spanText = new SpannableString(text);
spanText.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanText.setSpan(new TextAppearanceSpan(this, R.style.text_style), 5, text.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanText.setSpan(new ForegroundColorSpan(Color.RED), text.length() - 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(spanText);
Style文件: