广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程实现TextView字体颜色设置的方法小结
  • 229
分享到

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

小结方法Android 2022-06-06 09:06:53 229人浏览 安东尼
摘要

本文实例讲述了Android编程实现TextView字体颜色设置的方法。分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值。例如

本文实例讲述了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">地址://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 TextView字体颜色设置方法小结Android中EditText和AutoCompleteTextView设置文字选中颜色方法Android使用selector修改TextView中字体颜色和背景色的方法Android编程设置TextView颜色setTextColor用法实例Android TextView设置不同的颜色字体Android编程实现TextView部分颜色变动的方法Android编程实现TextView部分颜色变动的方法Android Textview实现颜色渐变滚动效果Android 实现不同字体颜色的TextView实现代码Android中TextView实现分段显示不同颜色的字符串


--结束END--

本文标题: Android编程实现TextView字体颜色设置的方法小结

本文链接: https://www.lsjlt.com/news/26254.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作