SoFunction
Updated on 2025-03-08

Summary of the usage examples of TextView controls in Android development

This article summarizes the usage of TextView controls for Android development. Share it for your reference, as follows:

The TextView control can display text information to the user, and we can set whether the text information can be edited.

1. Basic use of TextView

Create a TextView object in a program

Layout using in xml file

2、New Android Project->

Project name:TextView

Build Target:Android 2.2

Application name:TextViewDemo

Package name:com.

Create Activity:MainActivity

Min SDK Version:8

Finish

This way our project is built

3. Add TextView control to the file

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
       super(savedInstanceState);
       setContentView();
       TextView tv=new TextView(this);
       (“hello");
       setContentView(tv);
    }
}

This program is relatively simple, with only one control. If there are many controls, we need to write a lot of code to layout the controls, which will bring great difficulties to our future maintenance. Therefore, in Android development, it is recommended to use xml files for control layout

4. Add controls to the file

<TextView
   android:
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Hello"
/>

Run the program again, and "Hello" will output on the simulator

If many people use our program, the appearance must be beautiful. If placed in the XML file, we can easily modify its appearance. At the same time, the artists design the XML file, and the programmers are only responsible for coding. The artists do not care about how the code is designed. This is very similar to web development. The artists focus on making pages, and those who do the background focus on designing the background code, and they do not interfere with each other.

5. TextView attribute

It is recommended to use sp as unit to set the font size

When setting properties such as width or height, dp(dip) is recommended as the unit.

android:TextSize="20sp"

6. Set up a hyperlink

android:autoLink when setting whether it is a text URL link/email/phone number/map, the text is displayed as a clickable link

android:autoLink="phone"

7. Set the font color

android:textColor="#00FF00"

8. Marionette effect

android:ellipSize setting when the text is too long, how does the control display?

start—ellipsis is displayed at the beginning

end—ellipsis is displayed at the end

middle—ellipsis is displayed in the middle

marquee—displayed in a marquee

<!--Number of runs-->

android:marqueeRepeatLimit="marquee_forever"

<!--Get focus when touched->

android:focuseableTouchMode="true"

<!-- Single line display->

android:singleLine="true"

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《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.