法一:

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="你好,世界"
/>



更多相关文章

  1. Android(安卓)cocos2d 弹弓游戏 Catapult 源代码 完成
  2. Android(安卓)对话框中的进度条 ProgressDialog
  3. 【COCOS2DX-ANDROID-游戏开发之二三】 界面中嵌入Android的WebVi
  4. android intent 传递list或者对象
  5. Android源码学习之五-Android的IPC机制
  6. Android之HttpURLConnection小结
  7. Android之使用PackageManager取得程序的包名、图标等
  8. 阅读《Android(安卓)从入门到精通》(?)——View 和 ViewGroup
  9. android jetpack 简单livedata和viewmodel

随机推荐

  1. mulator: ERROR: no search paths found
  2. Android的SandBox(沙箱)
  3. Android:windowSoftInputMethod软键盘弹出
  4. 8步打开android之门 NDK入门
  5. 如何在Mac上开发Android应用(原创,给刚接
  6. android中的测试
  7. Android动画学习Demo(2) 关于Property An
  8. 带你走进Android(安卓)Afinal框架的世界
  9. Android 开发手记一 NDK编程实例
  10. Cordova插件调用Android原生Activity