为TextView控件显示的字符串设置下划线
方法一:
在string.xml中添加字符串并在布局.xml中设置
<resources>
......
<string name="underline"><u>underline</u></string>
......
</resources>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/underline"/>
注意,该方法添加,在Preview里是看不到下划线的,真机或模拟器运行后就看到了。
方法二:
代码中设置
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(Html.fromHtml("只有<u>underline</u>会有下划线"));//<u>标签内的字符串会被添加下划线
或者
textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); //设置下划线(整个字符串设置)
textView.getPaint().setAntiAlias(true);//抗锯齿
方法三:
使用SpannableString
方法来进行设置,使用SpannableString
方法能够得到更多的字符串效果,这里只使用设置下划线的方法。
SpannableString ss = new SpannableString("为这段文字设置下划线");
ss.setSpan(new UnderlineSpan(),1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //为第1-3字符设置下划线
ss.setSpan(new UnderlineSpan(), 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //为第4-5字符设置下划线
textView.setText(ss);