100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android TextView设置字体颜色

android TextView设置字体颜色

时间:2022-04-30 05:39:29

相关推荐

android TextView设置字体颜色

今天,简单讲讲如何设置TextView的字体颜色。

其实很简单,不过之前忘记了,所以还是记录,总结一下。

TextView的字体设置方法:

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

2、在Activity类中进行设置

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

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="/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">地址:/</string> <string name="app_name">脚本之家</string> </resources>

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

这个很简单,接下来讲讲如何在代码里设置TextView的字体颜色

第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>

接下来可以看看android 的setTextColor的源码:

android中设置TextView的颜色有方法setTextColor,这个方法被重载了,可以传入两种参数

public void setTextColor(int color) { mTextColor = ColorStateList.valueOf(color); updateTextColors(); } public void setTextColor(ColorStateList colors) { if (colors == null) { throw new NullPointerException(); } mTextColor = colors; updateTextColors(); }

第一个publicvoidsetTextColor(intcolor)要注意传入的必须是#FF0000这样类型的参数,这种方法也就是传入int color值,这个int不是R文件中自动分配的int值,所以要注意.

不要这样传入参数tv.setTextColor(R.color.red); 这样的做法就这错误的,我就犯过这样的错误,正确的做法是tv.setTextColor(this.getResources().getColor(R.color.red));

也可以直接使用Color类中的静态方法构造出来的颜色int值。

TextView tv = new TextView(this); tv.setText("Test set TextView's color."); //方案一:代码中通过argb值的方式 tv.setTextColor(Color.rgb(255, 255, 255));

第二种代码是获取String.xml文件的color值

Resources resource = (Resources) getBaseContext().getResources(); ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color); if (csl != null) { tv.setTextColor(csl); }

这种方法是通过ColorStateList得到xml中的配置的颜色的。好多需要xml中配置的都要类似这样的映射xml文件。

还有种方法Java代码:

XmlResourceParser xrp = getResources().getXml(R.color.my_color); try { ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp); tv.setTextColor(csl); } catch (Exception e) { }

String.xml文件为:

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ListViewDemoActivity!</string> <string name="app_name">ListViewDemo</string> <color name="my_color">#FFFFFF</color> </resources>

android TextView设置字体颜色就讲完了。

就这么简单。



本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。