Android编程实现TextView字体颜色设置的方法小结

2019-12-10 19:50:27刘景俊
易采站长站为您分析Android编程实现TextView字体颜色设置的方法,结合实例形式总结分析了Android针对TextView字体颜色设置的相关步骤与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下  

本文实例讲述了Android编程实现TextView字体颜色设置的方法。,具体如下:

对于setTextView(int a)这里的a是传进去颜色的值。例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去。

复制代码 tv.setTextColor(this.getResources().getColor(R.color.red));
关键字: android textview color

 

TextView的字体设置方法:

1、直接通过配置文件设置

2、在Activity类中进行设置

第一种方式很简单,用于静态或初始文字颜色的设置,方法如下:

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:background="@drawable/white" 
  > 
<TextView  
  android:id="@+id/tv01" 
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:text="@string/hello" 
  android:autoLink="all" 
  android:textColor="@color/red" 
  /> 
</LinearLayout>

color.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <drawable name="white">#FFFFFF</drawable> 
  <drawable name="dark">#000000</drawable> 
  <drawable name="red">#FF0000</drawable> 
</resources> 
strings.xml
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <string name="hello">地址:http://www.easck.com/</string> 
  <string name="app_name">脚本之家</string> 
</resources>