zoukankan      html  css  js  c++  java
  • android textview改变部分文字的颜色和string.xml中文字的替换及部分内容设置颜色、字体、超链接、图片

    一:TextView组件改变部分文字的颜色:

    1.TextView textView = (TextView)findViewById(R.id.textview);  
    2.  
    3.//方法一:  
    4.textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));  
    5.  
    6.//方法二:  
    7. String text = "获得银宝箱!";  
    8. SpannableStringBuilder style=new SpannableStringBuilder(text);     
    9.  style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置textview的背景颜色  
    10.  style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色  
    11.  textView.setText(style);  

    二:android string.xml文件中的整型和string型代替:

    String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");  

    对应的string.xml文件参数:

    <string name="baoxiang">您今天打了%1$d局,还差%2$d局可获得%3$s!</string>  
    %1$d表达的意思是整个name=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一个整型%1$d表达的意思是整个name=”<span style="white-space: pre;">baoxiang</span>”字符串中,第一个整型

    在项目开发者,经常需要把以上两者结合起来使用。可以避免很多textview的拼接,如下所示:

    1.TextView textView = (TextView)findViewById(R.id.testview);  
    2.  
    3.String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");  
    4.       int index[] = new int[3];  
    5.       index[0] = text.indexOf("2");  
    6.       index[1] = text.indexOf("18");  
    7.       index[2] = text.indexOf("银宝箱");  
    8.  
    9. SpannableStringBuilder style=new SpannableStringBuilder(text);     
    10.           style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
    11.           style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
    12.           style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
    13.           textView.setText(style);
     //android TextView、EditText对部分内容设置颜色、字体、超链接、图片; 
    //这里是以一个TextView为例子,EditText的设置方法和TextView一样

    //TextView对象
    TextView txtInfo = new TextView(this);

    //文本内容
    SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");

    //设置0-2的字符颜色
    ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //设置2-5的字符链接到电话簿,点击时触发拨号
    ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //设置9-11的字符为网络链接,点击时打开页面
    ss.setSpan(new URLSpan("http://www.hao123.com"), 9, 11,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //设置13-15的字符点击时,转到写短信的界面,发送对象为10086
    ss.setSpan(new URLSpan("sms:10086"), 13, 15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //粗体
     ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //斜体
    ss.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 7, 10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //下划线
    ss.setSpan(new UnderlineSpan(), 10, 16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    //以下代码是在指定位置插入图片
    Drawable d = getResources().getDrawable(R.drawable.icon);

    //设置图片大小
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

    //插入的位置
    ss.setSpan(new ImageSpan(d, ImageSpan.ALIGN_BASELINE), 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    //设置文本内容到textView
    txtInfo.setText(ss);

    //不添加这一句,拨号,http,发短信的超链接不能执行.
    txtInfo.setMovementMethod(LinkMovementMethod.getInstance());
     
     
  • 相关阅读:
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale
    CodeForces 785B Anton and Classes
    CodeForces 785A Anton and Polyhedrons
    爱奇艺全国高校算法大赛初赛C
    爱奇艺全国高校算法大赛初赛B
    爱奇艺全国高校算法大赛初赛A
    EOJ 3265 七巧板
    EOJ 3256 拼音魔法
    EOJ 3262 黑心啤酒厂
  • 原文地址:https://www.cnblogs.com/622698abc/p/3044363.html
Copyright © 2011-2022 走看看