淘宝货源网,含有近反义词的成语,沈阳最新油价
本文实例讲述了android编程实现textview字体颜色设置的方法。分享给大家供大家参考,具体如下:
对于settextview(int a)这里的a是传进去颜色的值。例如,红色0xff0000是指0xff0000如何直接传入r.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去。
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">地址://www.jb51.net/</string> <string name="app_name">移动技术网</string> </resources>
上面将资源部分分成了3个部分,目的是为了清晰,当然你也可以只建一个xml文件放在res目录下,而且文件名称可以随便命名。
注意两个地方:
1、main.xml的textview标签中:
/red指在标签下找其name值为red的内容,此时其值为#ff0000
因此,这里我们还可以这样做:
/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种:
第3种:
<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 apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论