代码中更改 TextView 文字颜色
2013-12-21 09:30
190 查看
①
创建工程
②
编写布局 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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:text="TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="这里使用Graphics颜色静态常量"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
③
新加 drawable.xml,其中添加一个 white 颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffffff</color>
</resources>
④
在代码中由 ID 获取 TextView
TextView text_A=(TextView)findViewById(R.id.TextView01);
TextView text_B=(TextView)findViewById(R.id.TextView02);
⑤
获取 Resources 对象
Resources myColor_R=getBaseContext().getResources();
⑥
获取 Drawable 对象
Drawable myColor_D=myColor_R.getDrawable(R.color.white);
⑦
设置文本背景颜色
text_A.setBackgroundDrawable(myColor_D);
⑧
利用 android.graphics.Color 的颜色静态变量来改变文本颜色
text_A.setTextColor(android.graphics.Color.GREEN);
⑨
利用 Color 的静态常量来设置文本颜色
text_B.setTextColor(Color.RED);
创建工程
②
编写布局 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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:text="TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="这里使用Graphics颜色静态常量"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
③
新加 drawable.xml,其中添加一个 white 颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffffff</color>
</resources>
④
在代码中由 ID 获取 TextView
TextView text_A=(TextView)findViewById(R.id.TextView01);
TextView text_B=(TextView)findViewById(R.id.TextView02);
⑤
获取 Resources 对象
Resources myColor_R=getBaseContext().getResources();
⑥
获取 Drawable 对象
Drawable myColor_D=myColor_R.getDrawable(R.color.white);
⑦
设置文本背景颜色
text_A.setBackgroundDrawable(myColor_D);
⑧
利用 android.graphics.Color 的颜色静态变量来改变文本颜色
text_A.setTextColor(android.graphics.Color.GREEN);
⑨
利用 Color 的静态常量来设置文本颜色
text_B.setTextColor(Color.RED);
相关文章推荐
- 在带(继承)TextView的控件中,在代码中动态更改TextView的文字颜色
- 更改TextView文字颜色,引用Drawable颜色常数及后台色
- android-代码修改TextView中部分某段文字的颜色
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- Android 更改TextView文字颜色(引用Drawable颜色常数及背景色)
- Android代码中更改TextView颜色
- 更改TextView文字颜色
- Android 代码里动态设置TextView/Button等的文字颜色Seletor
- 在代码中更改TextView中字体的颜色
- Android学习笔记(三) —— 更改TextView文字颜色及文字背景色
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- android listview item点击时更改textview的颜色 代码中实现
- 更改TextView文字颜色-------引用Drawable颜色常数及背景色
- 代码中设置textView的颜色出现文字消失或颜色设置不上的问题
- 在代码中将TextView的文字颜色改为自己写的color.xml文件设置的颜色
- 更改TextView文字颜色
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- 更改TextView文字颜色——引用color.xml中的颜色常数
- 设置TextView文字的颜色
- 关于一个Textview内的文字设置不同大小和颜色