继《android TextView的字体颜色设置的多种方法》【http://yahaitt.iteye.com/blog/454439】
下面看看第二种方式:在Activity类中进行设置
1、先将main.xml改成如下,即去掉android:textColor="@color/red":
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"
/>
</LinearLayout>
<?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,原始代码如下:
Java代码
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);
}
}
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>
详细的代码如下:
Java代码
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 {
/** Called when the activity is first created. */
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);
/*
Resources rs = this.getResources();
tv.setTextColor(rs.getColor(R.drawable.red));
*/
}
}
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 {
/** Called when the activity is first created. */
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);
/*
Resources rs = this.getResources();
tv.setTextColor(rs.getColor(R.drawable.red));
*/
}
}
注:请切换相应的注释
通过在Activity类中设置文本颜色,我们可以实现文本颜色的动态化。如果想保持文本颜色静态不变的话,可以直接通过上一篇中讲的通过直接配置即可。
分享到:
相关推荐
Android 一个TextView中设置不同字体大小和颜色
android实现TextView文字部分渐变色;LinearGradientFontSpan SpannableString
本文实例讲述了Android编程中TextView字体属性设置方法(大小、字体、下划线、背景色)。分享给大家供大家参考,具体如下: import android.content.Context; import android.graphics.Color; import android.text....
Android TextView 个别文字字体颜色格式
NULL 博文链接:https://aichixihongshi.iteye.com/blog/1206525
主要介绍了Android TextView字体颜色设置方法,结合实例形式总结分析了Android开发中TextView设置字体颜色的常用技巧,需要的朋友可以参考下
主要介绍了Android编程实现TextView字体颜色设置的方法,结合实例形式总结分析了Android针对TextView字体颜色设置的相关步骤与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
Android 设置textview某段字体颜色或者背景
实现textview字体颜色的渐变显示
android TextView android TextView
Android 文本类中字体颜色的改变,并可以相应点击事件
如果我们的应用需要显示时间,可能首先想到大街上LED显示的绿色的发光字体, 想到 使用时钟控件显示时间,这种效果,我们用 TextView 就可以实现。具体看博客说明:编辑文章 - 博客频道 - CSDN.NET ...
Android TextView 去掉自适应默认的fontpadding的实现方法 最近在项目中使用textview时发现在使用Android:layout_height=”wrap_content”这个属性设置后,textview会有默认的padding,也就是fontpadding。这样就会...
本文实例讲述了Android TextView实现垂直滚动效果的方法。分享给大家供大家参考,具体如下: 在TextView中,如果文本很长,可能需要实现垂直滚动显示文本的效果。这里需要在XML布局文件中为TextView设置如下几个属性...
给TextView设置字体样式的三种方法
Android SpannableString实现TextView不同大小文字居中显示。
通过textview设置状态选择