您的位置:首页 > 移动开发 > Android开发

Android中TextView的那些事

2016-04-22 10:51 549 查看
1、TextView中多个动态数据拼接:%1$s 其中1代表数字,s 代表类型(字符串类型) d整型

<string name="game_lefttime">%1$s天%2$s时%3$s分%4$s秒</string>


在代码中进行动态赋值:


String leftOriStr = res.getString(R.string.game_lefttime, nums[0],
nums[1], nums[2], nums[3]);//res 为资源对象


2、TextView中设置同一个textView中不同颜色:

String html="TextVie显示html 字体颜色为"+"<font color ='red'>红色</font><br/>";
tv3.setText(Html.fromHtml(html));


3、TextView中设置同一个textview中不同颜色,不同字体大小:SpannableString

/**
* 橘红色,content为字符串,特殊字符起止位置
* */
public static SpannableString diffSizeAndColor(Context context,String content,int start,int end){
SpannableString styledTextM = new SpannableString(content);
styledTextM.setSpan(new TextAppearanceSpan(context, R.style.style_money_orange), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return styledTextM;
}
tv.setText(diffSizeAndColor(getContext(),strM,5,strM.length()-1),TextView.BufferType.SPANNABLE);


<style name="style_money_orange">
<item name="android:textSize">@dimen/fontSize_1</item>
<item name="android:textColor">@color/color_10</item>
</style>


4、TextView跑马灯效果:

<TextView
android:id="@+id/tv12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="跑马灯效果  跑马灯效果  跑马灯效果  跑马灯效果"/>


5、TextView中部分文本的点击事件:ClickableSpan

在使用ClickableSpan的时候,在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象。

SpannableString spannableClickString = new SpannableString("TextView设置点击事件Span") ;
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,"TextView设置点击事件Span", Toast.LENGTH_LONG).show();
}
};
spannableClickString.setSpan(clickableSpan,11,15, Spannable.SPAN_EXCLUSIVE_INCLUSIVE) ;
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(spannableClickString);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息