Android - set TextView TextStyle programmatically?

前端 未结 11 634
南旧
南旧 2020-12-02 05:04

Is there a way to set the textStyle attribute of a TextView programmatically? There doesn\'t appear to be a setTextStyle() method.

11条回答
  •  囚心锁ツ
    2020-12-02 05:41

    Since setTextAppearance(resId) is only available for API 23 and above, use:

    TextViewCompat.setTextAppearance(textViewGoesHere, resId)

    This method is internally implemented as follows:

    public static void setTextAppearance(@NonNull TextView textView, @StyleRes int resId) {
        if (Build.VERSION.SDK_INT >= 23) {
            textView.setTextAppearance(resId);
        } else {
            textView.setTextAppearance(textView.getContext(), resId);
        }
    }
    

提交回复
热议问题