SoFunction
Updated on 2025-03-01

Android TextView font color setting method summary

This example summarizes the Android TextView font color setting method. Share it for your reference, as follows:

For setTextView(int a) Here a is the value of the color passed in. For example, red 0xff0000 means that there is no way to set the color when passing 0xff0000 directly. You can only obtain the color value of the resource first through the third method in the article and then pass it in.

(().getColor());

Keywords: android textview color

TextView font setting method:

1. Set it directly through the configuration file
2. Set it in the Activity class

The first method is very simple, and is used to set static or initial text colors, as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
<TextView
  android:
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  android:autoLink="all"
  android:textColor="@color/red"
  />
</LinearLayout>

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
  &lt;drawable name="white"&gt;#FFFFFF&lt;/drawable&gt;
  &lt;drawable name="dark"&gt;#000000&lt;/drawable&gt;
  &lt;drawable name="red"&gt;#FF0000&lt;/drawable&gt;
&lt;/resources&gt;

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
 &lt;string name="hello"&gt;address:&lt;/string&gt;
  &lt;string name="app_name"&gt;Yali's notebook&lt;/string&gt;
&lt;/resources&gt;

The resource part is divided into 3 parts, the purpose is to be clear. Of course, you can also create only one XML file and place it in the res directory, and the file name can be named at will.

Pay attention to two things:

1. In the TextView tag: android:textColor="@color/red"

2. Medium: <color name="red">#FF0000</color>

@color refers to obtaining the <color> tag in the resource file (all xml files under the res directory)

/red refers to looking for content with the name value red under the tag. At this time, its value is #FF0000

So here we can also do this: android:textColor="@drawable/red"

@drawable refers to obtaining the <drawable> tag in the resource file

/red refers to looking for content with the name value of red under the tag

By analogy, I believe you will know what to do if you are in the middle.

Let's take a look at the second method: set it in the Activity class

1. First change it to the following, that is, remove android:textColor="@color/red":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
<TextView
  android:
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  android:autoLink="all"
  />
</LinearLayout>

2. Modify the onCreate method of the Activity. Here my Activity is Study03_01. The original code is as follows:

package yahaitt.study03_01;
import ;
import ;
public class Study03_01 extends Activity {    @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
  }
}

Step 1: Obtain the text control TextView, named tv

Step 2: Set TextColor method of TextView, there are 3 ways to set the text color:

Type 1: ();//The color class that comes with the system

The second type: (0xffff00ff); //0xffff00ff is an int type data. Group it in 0x|ff|ff00ff, 0x is a mark representing color integers, ff is the transparency, ff00ff represents color, Note: Here ffff00ff must be a 8 color representation, and 6 color representations such as ff00ff are not accepted.

The third type: (().getColor());//Set by obtaining the resource file. Depending on different situations, it can also be or, of course, the premise is that the corresponding configuration needs to be made in the corresponding configuration file, such as:

<color name="red">#FF0000</color>
<drawable name="red">#FF0000</drawable>
<string name="red">#FF0000</string>

The detailed code is as follows:

package yahaitt.study03_01;
import ;
import ;
import ;
import ;
import ;
public class Study03_01 extends Activity {
  private TextView tv;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    tv = (TextView)(.tv01);
    //();
    //(0xff000000);
  }
}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android communication methods summary》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.