android改变字体的颜色的三种方法
法一:
TextView tv = new TextView(this);
tv.setText("你好");
setContentView(tv);
法二:
/**
* testview是View的子类
* findViewById返回的是VIEW对象
* */
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("我<font color = red>勒个去</font>啊"));
main.xml配置文件:
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#00ffff"
android:text="你好,世界"
/>
法三:
TextView tv = (TextView)findViewById(R.id.tv);
String str = "我勒个去";
//创建一个style对象
SpannableStringBuilder style = new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.WHITE), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
main.xml配置文件:
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:textColor="#00ffff"
android:text="你好,世界"
/>
更多相关文章
- Android(安卓)cocos2d 弹弓游戏 Catapult 源代码 完成
- Android(安卓)对话框中的进度条 ProgressDialog
- 【COCOS2DX-ANDROID-游戏开发之二三】 界面中嵌入Android的WebVi
- android intent 传递list或者对象
- Android源码学习之五-Android的IPC机制
- Android之HttpURLConnection小结
- Android之使用PackageManager取得程序的包名、图标等
- 阅读《Android(安卓)从入门到精通》(?)——View 和 ViewGroup
- android jetpack 简单livedata和viewmodel