Color About——First

时间:2023-03-08 20:12:49

最近在Android开发学习中遇到TextView背景色以及文字颜色着色问题,在此做个记录。

首先对于颜色的选择,我推荐W3C School,上面有对网页颜色的详尽说明以及实例,还提供了对于,同一种颜色下搭配各种颜色的效果,链接如下:

http://www.w3school.com.cn/tags/html_ref_colornames.asp

在这里提供如下的颜色供参考:

http://www.qqtz.com/hao/color.htm

选择好你喜欢的颜色后,下面来简要说一下在Android开发中设置:

在设计好布局文件后,比如我添加了一个TextView,其名字我设置为secActTxtV,那么我设置其背景为#D2691E,那么我只需加入下面的代码即可:

 1: secActTxtV.setBackgroundColor(android.graphics.Color.parseColor("#D2691E"));

其中的你只需要将secActTxtV改成你的TextView控件

secActTxtV.setBackgroundColor(android.graphics.Color.parseColor("#D2691E"));

名,最后的#D2691E改成你喜欢的颜色值,其他保持默认即可.

Android对于设置背景颜色有好几种方式,本人比较喜欢上面我说的那一中parseColor()的方法。在Android官网中对其使用时这样描述的:

public static int parseColor (String colorString)

Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are:

用于解析颜色字符串,返回对应的颜色整型值。如果字符串不能够被解析,将会抛出IllegalArgumentException异常。

该方法支持的字符串格式如下:

RGB格式:

#RRGGBB

ARGB格式:

#AARRGGBB

颜色名:

'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray'

'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuschia', 'lime', 'maroon', 'navy', 'olive'

'purple', 'silver', 'teal'

背景色搞定后,我们来解决一下文字着色的问题:

这个我们可以在xml布局文件中进行设置,以我的为例:

我想设置文本颜色为7FFF00,那么只需找到TextView布局文件,在其中加入一句:

  1: android:textColor="#7FFF00"

即可完成文本的着色。

对于文本的着色,在这里多说一点儿。其实我们还可以使用HTML的知识来为文本着色。仍然以上面的secActTxtV为例。可以使用下面的方法:

thdActTxtV.setText(Html.fromHtml(String s))

其中我们可以构造字符串s为html格式,进而使的颜色文字设置更加绚丽。在这里设定字符串

  1. str=
  2. "<font color='red'>学习态度</font>"  +
  3. "<font color= 'green'>Android Developer</font>"+
  4. "<a href=\"http://www.google.com\">知之为知之,不知Google知</a>”;

注:上面的链接中为了添加链接http://www.google.com,两边加上的双引号需要进行转义,即

"http://www.google.com"要写成\”http://www.google.com\” ,这里不要出错。除此之外,

如果你加入了链接,那么你要在相应位置加入下面的语句,以获得实例动作。具体语句为:

thdActTxtV.setMovementMethod(LinkMovementMethod.getInstance());

使用的时候,可以有这么几种使用方法:

A:可以直接建立一个方法进行使用,我这里设置方法名称为content(),如下:

  1. private void content(){
  2. String str = "<font color='red'>学习态度:</font>"  + "<font color= 'green'>Android Developer</font>"+
  3. "<a href=\"http://www.google.com\">知之为知之,不知Google知</a>";
  4. thdActTxtV.setText(Html.fromHtml(str));
  5. }

然后在使用content();后加入必要的语句,如下:

1: content();

2:thdActTxtV.setMovementMethod(LinkMovementMethod.getInstance());

B:你当然也可以像下面这样直接使用,而不用新建的方法:

  1. String str = "<font color='red'>学习态度:</font>"  +
  2. "<font color= 'green'>Android Developer</font>"+
  3. "<a href=\"http://www.google.com\">知之为知之,不知Google知</a>";
  4. thdActTxtV.setText(Html.fromHtml(str));
  5. thdActTxtV.setMovementMethod(LinkMovementMethod.getInstance());

对于Button按钮的文本以及背景均设置类似,不再赘述。

附录:

Android TextView 支持的HTML标签

  • <a href="...">
  • <b>
  • <big>
  • <blockquote>
  • <br>
  • <cite>
  • <dfn>
  • <div align="...">
  • <em>
  • <font size="..." color="..." face="...">
  • <h1>
  • <h2>
  • <h3>
  • <h4>
  • <h5>
  • <h6>
  • <i>
  • <img src="...">
  • <p>
  • <small>
  • <strike>
  • <strong>
  • <sub>
  • <sup>
  • <tt>
  • <u>

参考文章:

http://www.cnblogs.com/playing/archive/2011/03/17/1987033.html

http://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String)