Android study notes (4): TextView settings content, font size, color, background

         As shown in the figure, we can set the text font size, color, and background color in Android phones.

1. TextView setting content

        We have already mentioned in the previous chapter how to use the TextView module to display text content. Let’s briefly mention it here.

//strings.xml
<string name="hello">Hello World!</string>
<string name="android">hello Android!</string>

// activity_main.xml
<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

// MainActivity.java
TextView textView = findViewById(R.id.text1);
textView.setText(R.string.android);

        Presumably, through the previous study, you have been able to understand the meaning of the above code, and can create the corresponding value in the corresponding file. In the above code, two ways of setting content are given.

        The first method is to set the content via text= in the xml layout file.

        The second method is to use the setText function to set the content through the TextView object. Note that the priority of the class is greater than that of the layout file, that is, both contents are defined at the same time, and the content set in the class is ultimately displayed.

2. TextView sets font size

1. Font size unit

        Let’s first understand the font size unit:

px (pixel): It is the smallest display unit of the mobile phone screen. The mobile phone screen is composed of multiple pixels.

dp (device independent pixel): the same unit has different display effects on different devices. When you set the font size unit to dp, it will eventually be converted to px.

Now you don’t need to be too clear about the conversion relationship between them, as long as you know the following two characteristics of dp:

1. For mobile phones with the same resolution, the larger the screen size, the smaller the proportion of the screen occupied by components with the same dp.

2. For mobile phones of the same size and different resolutions, components with the same dp occupy the same proportion of the screen.

Therefore, we can see that when we use dp as the unit, as long as it is on a phone of the same size, regardless of its resolution, we can get a good fit. If you can't understand it clearly, you can use Android studio to create mobile phones of different sizes and resolutions for experimentation.

        In addition, there is sp, which is similar to dp. The difference is that its size will change according to the font size set by the system.

2. Set font size

// MainActivity.java
TextView textView2 = findViewById(R.id.text2);
textView2.setTextSize(30);

// activity_main.xml
<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:textSize="35sp"/>

        There are two ways to set the font size:

1. Use the setTextSize function to set the size through the TextView object. You can see that there is no need to give the unit here. The default unit is sp.

2. Set through xml layout file, textSize=  

3. TextView sets font color

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="35sp"
        android:textColor="#ff0000"/>

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="40sp"
        android:textColor="@color/red"/>

// colors.xml
    <color name="red">#FFFF0000</color>

        ​​​​​As you can see from the above code, we can set the font color through textColor from the xml layout file, or we can define the color in the res/values/colors.xml file and reference it through "@color/...". Among them, the value of the color is the # sign + a hexadecimal number, and the number can be 8 digits or 6 digits.

        For example, "#12345678", the digits are counted from left to right, the first and second digits "12" represent the transparency of the font, 00 represents completely transparent, ff represents completely opaque; the 3rd and 4th digits, 5th and 6th digits, Bits 7 and 8 respectively represent the three colors of red, green and blue. The higher the value they represent, the darker the color. For example: 00 00 00 black, ff 00 00 red, 00 ff 00 green, 00 00 ff blue, ff ff ff white. Among them, digits 1 and 2 can be omitted, and the system defaults to ff, which is completely opaque.

        TextView textView2 = findViewById(R.id.text2);
        textView2.setTextSize(30);
        textView2.setTextColor(0xff0000ff);
        //textView2.setTextColor(getResources().getColor(R.color.black));

        As shown in the above code, we can also set the font color in the java file, that is, call the setTextColor interface through the TextView object. Among them, the color is set here using an 8-digit hexadecimal number. If the first two digits are not written, the default is 00, which is completely transparent, which is opposite to the default setting in the xml file. In addition, we can also set the color by getting the color definition in the colors.xml file through getResources().getColor(R.color.black).

4. TextView sets font background color 

// xml
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="45sp"
        android:textColor="@color/white"
        android:background="@color/red"/>
// java
        TextView textView4 = findViewById(R.id.text4);
        //textView4.setBackgroundColor(Color.GREEN);
        textView4.setBackgroundResource(R.color.black);

        As shown in the code, setting the background color can also be set through xml or Java files. In the xml file, it is set by background; in java, it is set by setBackgroundColor. The passed-in parameters can be static member variables in the Color class, or static function interfaces to define colors. You can also set the color by obtaining the definition in the colors.xml file through the setBackgroundResource interface.

5. Summary 

        Through the study of this chapter, you will master how to set font content, size, color, and background through TextView to meet different needs.

Guess you like

Origin blog.csdn.net/CJ_study/article/details/133916549