Summarize the commonly used TextView styles in Android (SuperTextView)

Introduction: TextView generally displays copywriting in daily development, but in actual development, text often displays some special styles. In order to meet the needs of different scenarios, we hereby summarize the commonly used TextView and name it: SuperTextView

Scenario 1: Use TextView to display the terms of service and privacy agreement, and realize click-to-jump to H5.

Core code:

public class PrivacyPolicyTextActivity extends AppCompatActivity {

    private TextView mPrivacyPolicyView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_policy);
        mPrivacyPolicyView = findViewById(R.id.privacyPolicy);
        initPrivacyPolicy();
    }

    private void initPrivacyPolicy() {
        String contentString = getString(R.string.login_des_privacy_desc, getString(R.string.terms_of_service_desc), getString(R.string.privacy_policy_desc));
        String termsOfService = getString(R.string.terms_of_service_desc);
        SpannableString spannableString = new SpannableString(contentString);
        int termsOfIndex = contentString.indexOf(termsOfService);
        ClickableSpan termsOfClick = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(PrivacyPolicyTextActivity.this, WebViewActivity.class);
                intent.putExtra("url", "https://www.baidu.com/");
                intent.putExtra("title", termsOfService);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourceUtil.getColor(R.color.red_common_pure));
                ds.setUnderlineText(true);
            }
        };
        spannableString.setSpan(termsOfClick, termsOfIndex, termsOfIndex + termsOfService.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        String privacyPolicy = ResourceUtil.getString(R.string.privacy_policy_desc);
        int privacyIndex = contentString.indexOf(privacyPolicy);
        ClickableSpan privacyClick = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(PrivacyPolicyTextActivity.this, WebViewActivity.class);
                intent.putExtra("url", "https://www.baidu.com/");
                intent.putExtra("title", privacyPolicy);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ResourceUtil.getColor(R.color.red_common_pure));
                ds.setUnderlineText(true);
            }
        };
        spannableString.setSpan(privacyClick, privacyIndex, privacyIndex + privacyPolicy.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.red_common_pure));
        spannableString.setSpan(colorSpan, termsOfIndex, termsOfIndex + termsOfService.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.red_common_pure));
        spannableString.setSpan(colorSpan2, privacyIndex, privacyIndex + privacyPolicy.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        mPrivacyPolicyView.setText(spannableString);
        mPrivacyPolicyView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

 

 Scenario 2: In normal development, design often requires that some text in a paragraph should be bolded, displayed in different colors, etc. There are different methods at this time. The most commonly used and simplest one is to use HTML:

Core code:

public class HTMLTextActivity extends AppCompatActivity {

    private TextView mHtmlTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_htmltext);
        mHtmlTextView = findViewById(R.id.html_text);
        String str1 = "<font color='#FFF258'><b>" + "程序猿" + "</b> </font>";
        String str2 = "<font color='#03DAC5'><b>" + "北京" + "</b> </font>";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mHtmlTextView.setText(Html.fromHtml(ResourceUtil.getString(R.string.html_message, str1, str2), Html.FROM_HTML_MODE_LEGACY));
        } else {
            mHtmlTextView.setText(Html.fromHtml(ResourceUtil.getString(R.string.html_message, str1, str2)));
        }
    }
}

 Scene 3: Some pictures are often interspersed with the text, which is the style of text plus pictures:

Code:

public class ImageTextViewActivity extends AppCompatActivity {

    private TextView mTitleTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_text_view);
        mTitleTextView = findViewById(R.id.tv_title);
        String texValue01 = " 你好 ";
        String texValue02 = " 我在北京 ";
        String allTexValue = "A" + texValue01 + "B" + texValue02 + "C";
        int tex01ValueIndex = allTexValue.indexOf(texValue01);
        int tex01ValueEndIndex = tex01ValueIndex + texValue01.length();
        int tex02ValueIndex = allTexValue.indexOf(texValue02);
        int tex02ValueEndIndex = tex02ValueIndex + texValue02.length();
        SpannableString spannableString = new SpannableString(allTexValue);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ResourceUtil.getColor(R.color.red_common_pure));
        spannableString.setSpan(colorSpan, tex01ValueIndex, tex01ValueEndIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(ResourceUtil.getColor(R.color.teal_200));
        spannableString.setSpan(colorSpan2, tex02ValueIndex, tex02ValueEndIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ImageSpan imgSpan = new ImageSpan(this, R.drawable.e_1);
        spannableString.setSpan(imgSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ImageSpan imgSpan2 = new ImageSpan(this, R.drawable.e_2);
        spannableString.setSpan(imgSpan2, tex01ValueEndIndex, tex01ValueEndIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ImageSpan imgSpan3 = new ImageSpan(this, R.drawable.e_3);
        spannableString.setSpan(imgSpan3, tex02ValueEndIndex, tex02ValueEndIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTitleTextView.setText(spannableString);
    }
}

 Scenario 4: A special UI style is required in the text. At this time, you can add a View to the TextView.

public class TagTextViewActivity extends AppCompatActivity {

    private TextView mTitleTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag_text_view);
        mTitleTextView = findViewById(R.id.tv_title);
        View markerView = LayoutInflater.from(this).inflate(R.layout.item_marker_fore_title, null);
        SpannableStringBuilder builder = new SpannableStringBuilder();
        final String REPLACE_TEXT = "A";
        builder.append(REPLACE_TEXT);
        builder.setSpan(new MarkerViewSpan(markerView), 0, REPLACE_TEXT.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        String oriText = "你好,我在北京";
        builder.append(oriText);
        mTitleTextView.setText(builder);
    }
}

  代码 GithHub: GitHub - JasonZhangHG/SuperTextView: SuperTextViewSuperTextView. Contribute to JasonZhangHG/SuperTextView development by creating an account on GitHub.https://github.com/JasonZhangHG/SuperTextView

Guess you like

Origin blog.csdn.net/Jason_HD/article/details/126787634