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

 更新时间:2015年12月01日 14:44:19   作者:fengyee_zju  
这篇文章主要介绍了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://schemas.android.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">地址:https://www.jb51.net/</string> 
  <string name="app_name">脚本之家</string> 
</resources>

上面将资源部分分成了3个部分,目的是为了清晰,当然你也可以只建一个xml文件放在res目录下,而且文件名称可以随便命名。

注意两个地方:

1、main.xml的TextView标签中:

复制代码 代码如下:
android:textColor="@color/red"

2、color.xml中:
复制代码 代码如下:
<color name="red">#FF0000</color>

@color指获取资源文件中(所有res目录下的xml文件)的<color>标签

/red指在标签下找其name值为red的内容,此时其值为#FF0000

因此,这里我们还可以这样做:

复制代码 代码如下:
android:textColor="@drawable/red"

@drawable指获取资源文件中<drawable>标签

/red指在标签下找其name值为red的内容

以此类推,相信你也就知道了如果是在strings.xml中该怎么做了。

下面看看第二种方式:在Activity类中进行设置

1、先将main.xml改成如下,即去掉android:textColor="@color/red":

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.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" 
  /> 
</LinearLayout>

2、修改Activity的onCreate方法,这里我的Activity是Study03_01,原始代码如下:

package yahaitt.study03_01;  
import android.app.Activity; 
import android.os.Bundle; 
public class Study03_01 extends Activity {    @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
  } 
}

第一步:获得文本控件TextView,取名为tv

第二步:通过TextView的setTextColor方法进行文本颜色的设置,这里可以有3种方式进行设置:

第1种:

复制代码 代码如下:
tv.setTextColor(android.graphics.Color.RED);//系统自带的颜色类

第2种:
复制代码 代码如下:
tv.setTextColor(0xffff00ff);//0xffff00ff是int类型的数据
分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示。

第3种:

复制代码 代码如下:
tv.setTextColor(this.getResources().getColor(R.color.red));//通过获得资源文件进行设置。
根据不同的情况R.color.red也可以是R.string.red或者R.drawable.red,当然前提是需要在相应的配置文件里做相应的配置,如:

<color name="red">#FF0000</color>
<drawable name="red">#FF0000</drawable>
<string name="red">#FF0000</string>

详细的代码如下:

package yahaitt.study03_01; 
import android.app.Activity; 
import android.content.res.Resources; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.widget.TextView; 
public class Study03_01 extends Activity {  
  private TextView tv; 
  @Override 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
    tv = (TextView)this.findViewById(R.id.tv01);
    //tv.setTextColor(Color.RED);
    //tv.setTextColor(0xff000000);
  } 
}

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android自定义Animation实现View摇摆效果

    Android自定义Animation实现View摇摆效果

    这篇文章主要为大家详细介绍了Android自定义Animation实现View摇摆效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android实现NFC读取校园卡

    Android实现NFC读取校园卡

    这篇文章主要为大家详细介绍了Android实现NFC读取校园卡,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Android View的事件分发详解

    Android View的事件分发详解

    我们在学习View的时候,不可避免会遇到事件的分发,而往往遇到的很多滑动冲突的问题都是由于处理事件分发时不恰当所造成的。因此,深入了解View事件分发机制的原理,对于我们来说是很有必要的。
    2017-12-12
  • 深入分析Android NFC技术 android nfc开发

    深入分析Android NFC技术 android nfc开发

    本篇文章我们对android开发中nfc技术做了全面的原理分析以及实现过程,需要的读者们一起参考一下吧。
    2017-11-11
  • Android星级评分条控件RatingBar使用详解

    Android星级评分条控件RatingBar使用详解

    这篇文章主要为大家详细介绍了Android星级评分条控件RatingBar的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Android自定义View实现角度选择器

    Android自定义View实现角度选择器

    前几天在Google Photos查看照片,用了一下它的图片剪裁功能,于是我马上就被其界面和操作吸引。后来想模仿做一个和Google Photos裁图页面几乎一模一样的角度选择器,本文比较基础,在阅读本文前只需要掌握最基础的自定义View知识和Android事件知识。下面来一起学习下吧。
    2016-11-11
  • Android改变ExpandableListView的indicator图标实现方法

    Android改变ExpandableListView的indicator图标实现方法

    这篇文章主要介绍了Android改变ExpandableListView的indicator图标实现方法,结合实例形式分析了改变ExpandableListView的indicator图标相关步骤与实现技巧,涉及Android配置文件的修改,需要的朋友可以参考下
    2016-03-03
  • Android ActionBar完全解析使用官方推荐的最佳导航栏(上)

    Android ActionBar完全解析使用官方推荐的最佳导航栏(上)

    Action Bar是一种新増的导航栏功能,在Android 3.0之后加入到系统的API当中,它标识了用户当前操作界面的位置,并提供了额外的用户动作、界面导航等功能
    2017-04-04
  • Windows下快速搭建安卓开发环境Android studio

    Windows下快速搭建安卓开发环境Android studio

    这篇文章主要介绍了Windows下快速搭建安卓开发环境Android studio的相关资料,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android编程中沉浸式状态栏的三种实现方式详解

    Android编程中沉浸式状态栏的三种实现方式详解

    这篇文章主要介绍了Android编程中沉浸式状态栏的三种实现方式,简单描述了沉浸式状态栏的概念、功能并结合实例形式详细分析了Android实现沉浸式状态栏的三种操作技巧与注意事项,需要的朋友可以参考下
    2018-02-02

最新评论