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">地址:http://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程序设计有所帮助。

时间: 2024-07-28 17:38:43

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

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

本文实例讲述了Android编程实现TextView字体颜色设置的方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. 复制代码 代码如下: tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android t

Android编程中TextView字体属性设置方法(大小、字体、下划线、背景色)_Android

本文实例讲述了Android编程中TextView字体属性设置方法(大小.字体.下划线.背景色).分享给大家供大家参考,具体如下: import android.content.Context; import android.graphics.Color; import android.text.SpannableString; import android.text.Spanned; import android.text.style.AbsoluteSizeSpan; import andr

Android编程实现TextView部分颜色变动的方法_Android

本文实例讲述了Android编程实现TextView部分颜色变动的方法.分享给大家供大家参考,具体如下: public class StringHandleExampleActivity extends Activity { /** Called when the activity is first created. */ private TextView textView; private String tempStr = "abcd12我的中古zx9yu5!f3,,"; priva

Android编程实现TextView部分颜色变动的方法

本文实例讲述了Android编程实现TextView部分颜色变动的方法.分享给大家供大家参考,具体如下: public class StringHandleExampleActivity extends Activity { /** Called when the activity is first created. */ private TextView textView; private String tempStr = "abcd12我的中古zx9yu5!f3,,"; priva

Android TextView字体颜色设置方法小结_Android

本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

Android TextView字体颜色设置方法小结

本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

Android编程实现TextView垂直自动滚动功能【附demo源码下载】

本文实例讲述了Android编程实现TextView垂直自动滚动功能.分享给大家供大家参考,具体如下: 在做android 应用的开发的时候,横向滚动或者要做出跑马灯的效果很简单,textview本身的属性就支持,只要设置准确就会滚动,开发起来比较简单,但是textview 不支持垂直滚动,那么垂直滚动就需要自己来实现了,很多网友提供的垂直滚 动方案都是千篇一律,使用ScrollView来进行滚动,但是都不完美,做起来有些别扭.有一位网友给出的歌词的滚动思路明确,能从根本上解决问题,因此我实现的

Android系统更改状态栏字体颜色_Android

随着时代的发展,Android的状态栏都不是乌黑一片了,在Android4.4之后我们可以修改状态栏的颜色或者让我们自己的View延伸到状态栏下面.我们可以进行更多的定制化了,然而有的时候我们使用的是淡色的颜色比如白色,由于状态栏上面的文字为白色,这样的话状态栏上面的文字就无法看清了.因此本文提供一些解决方案,可以是MIUI6+,Flyme4+,Android6.0+支持切换状态栏的文字颜色为暗色.修改MIUI public static boolean setMiuiStatusBarDark

Android编程中TextView宽度过大导致Drawable无法居中问题解决方法_Android

本文实例讲述了Android编程中TextView宽度过大导致Drawable无法居中问题解决方法.分享给大家供大家参考,具体如下: 在做项目的时候,很多时候我们都要用到文字和图片一起显示,一般设置TextView的DrawableLeft.DrawableRight.DrawableTop.DrawableBottom就行了.但是有一种情况是当TextView的熟悉是fill_parent或者使用权重的时候并且设置了起Gravity的ceter的时候,Drawable图片是无法一起居中的,为了