android:为TextView添加样式——下划线,颜色,设置链接样式及前背景色

实现下划线及颜色设置:

public class AtActivity extends Activity {
LinearLayout ll;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ll=(LinearLayout)findViewById(R.id.ll);
        TextView tv=new TextView(this);
tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
        tv.setText("使用代码实现下划线样式");
        tv.setTextColor(Color.WHITE);
        ll.addView(tv);
        tv=new TextView(this);
tv.setText(Html.fromHtml("<u>使用html实现下划线样式</u>"));
        ll.addView(tv);
    }
}

设置超链接样式:默认的超链接是蓝色的,我们现在设置成前景红色

public class AtActivity extends Activity {
LinearLayout ll;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ll=(LinearLayout)findViewById(R.id.ll);
        TextView tv=new TextView(this);
        tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">自定义的超链接样式</a>"));
        tv.setMovementMethod(LinkMovementMethod.getInstance());  
        CharSequence text  =  tv.getText();
        if (text instanceof Spannable){ 
               int  end  =  text.length();   
              Spannable sp  =  (Spannable)tv.getText();   
              URLSpan[] urls = sp.getSpans( 0 , end, URLSpan.class );   
              SpannableStringBuilder style = new  SpannableStringBuilder(text);   
              style.clearSpans(); // should clear old spans    
               for (URLSpan url : urls){    
                  URLSpan myURLSpan=   new  URLSpan(url.getURL());                        
             style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
style.setSpan(new ForegroundColorSpan(Color.RED), sp.getSpanStart(url), sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//设置前景色为红色
               } 
               tv.setText(style);   
        }
        ll.addView(tv);
        tv=new TextView(this);
        tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">默认的超链接样式</a>"));
        tv.setMovementMethod(LinkMovementMethod.getInstance());  
        ll.addView(tv);
    }
}

 

转载自  http://blog.csdn.net/caiyunfreedom/article/details/6763834

=======================================================

android textview 下划线

在xml文件中使用android:textStyle="bold" 可以将英文设置成粗体,但是不能将中文设置成粗体,将中文设置成粗体的方法是: 
TextView tv = (TextView)findViewById(R.id.TextView01); 
TextPaint tp = tv.getPaint(); 
tp.setFakeBoldText(true); 
其他还有: 
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24f);//设置成24sp  
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//可能中文加粗无效 
textView.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//可能中文无效 
textView.setText(Html.fromHtml("<u>"+texts+"</u>"));//下划线 
textView.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);//斜体,中文有效 
textView.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );//中间加横线 
textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );//底部加横线

promotionLinkText = (TextView) this .findViewById(R.id. text_promotion_link ); 
中间加横线 
promotionLinkText .getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); 
底部加横线: 
promotionLinkText .getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );

http://www.cnblogs.com/umasuo/articles/android_TextView_underline.html

http://www.eoeandroid.com/archiver/tid-7310.html?page=3

 

转载自  http://www.cnblogs.com/jiezzy/archive/2012/11/09/2762812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值