zoukankan      html  css  js  c++  java
  • Android Span的简单使用

     Spanable中的常用常量:
     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含start和end所在的端点                 (a,b)
     Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点       (a,b]
     Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含start,但不包含end所在的端点          [a,b)
     Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含start和end所在的端点                       [a,b]

    1.改变字体的不同颜色

      String text = String.format("¥%1$s  门市价:¥%2$s", 18.6, 22);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            int z = text.lastIndexOf("门");
            /**
             *  ForegroundColorSpan设置前景的颜色
             */
            style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
            style.setSpan(new ForegroundColorSpan(Color.YELLOW), 2, 4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
            style.setSpan(new ForegroundColorSpan(Color.GREEN), 4, 6,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
            textView2.setText(style);

    2.改变字体大小 颜色 加换行

      /**
             * 改变字体大小  颜色 加换行
             */
            Spannable span = new SpannableString("2017"+"
    "+"-03-02");
            span.setSpan(new RelativeSizeSpan(1.5f), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(new RelativeSizeSpan(3.0f), 5, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(new ForegroundColorSpan(Color.RED), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            /**
             * BackgroundColorSpan设置背景色,为0代表不设置背景色默认为activity背景色
             */
            //span.setSpan(new BackgroundColorSpan(Color.WHITE), 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(0, 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView3.setText(span);

    3.带有URL的TextView文本

            String msg = "带有URL的TextView文本《点击这里跳转》";
            SpannableString smp = new SpannableString(msg);
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    startActivity(new Intent(MainActivity.this, Main2Activity.class));
                }
                @Override
                public void updateDrawState(TextPaint ds) {
                    /**
                     * 这里如果设置为false则不带下划线,true带有下划线
                     */
                    ds.setUnderlineText(false);
                }
            } ;
    
            /**
             * 设置点击的范围
             */
            smp.setSpan(clickableSpan, msg.indexOf("《") + 1, msg.lastIndexOf("》"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            /**
             * 设置前景色
             */
        smp.setSpan(new ForegroundColorSpan(Color.parseColor("#0AC3BC")), msg.indexOf("《"), msg.lastIndexOf("》") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView4.setText(smp);
            /**
             * 设置添加链接
             */
            textView4.setMovementMethod(LinkMovementMethod.getInstance());

    4.带有下划线的文本

            String content="带有下划线的文本";
            SpannableString ssb = new SpannableString(content);
            ssb.setSpan(new UnderlineSpan(), 0, 6, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            textView5.setText(ssb);
    今天多一点积累,明天少一分烦恼
  • 相关阅读:
    ThinkPHP框架返回插入记录的id号
    TP框架中关于if、else 分支结构逻辑错误
    SVN 快速入门!
    TP框架中如何使用SESSION限制登录?
    TP框架M方法 create方法丢失字段问题
    .NET Framework 工具
    X86-64寄存器和栈帧
    微软开源资源 NET Foundation Projects
    Import 元素 (MSBuild)
    C#开源资源项目
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/6490461.html
Copyright © 2011-2022 走看看