The concept of View and ViewGroup
Introduction to this section
Farewell to the first chapter and welcome to the second chapter - a detailed explanation of the UI (User Interface) components in Android. What we are going to learn in this section is the View and ViewGroup classes, the parent classes of all controls! On a whim, I directly translated the official document Let’s introduce these two things. By the way, the Chinese reason is that Google can’t access it and Android developer can’t access it. We can Change hosts or use a VPN proxy. Of course, you can also use domestic API mirrors like the author. Let’s share one here: http://androiddoc.qiniudn.com/guide/topics/ui/overview.html This mirror is a 5.0 API!
UI Overview
In Android APP, all user interface elements are composed of View and ViewGroup objects. A View is an object drawn on the screen that the user can interact with. ViewGroup is a layout container used to store other View (and ViewGroup) objects! Android provides us with a collection of View and ViewGroup subclasses, which provides some commonly used input controls (such as buttons and text fields) and various layout modes (such as linear or relative layout)
User Interface Layout
Every component in your app's user interface is built using a hierarchy of View and ViewGroup objects, such as figure 1. Each ViewGroup is an invisible container for organizing sub-Views, and its sub-Views may be input controls. Or a widget that draws a certain area on the UI. With the hierarchical tree, you can design it as simple or complex as you need. Complex layout (the simpler the layout, the better the performance)
Figure 1. Illustration of a UI layout hierarchy
To define your layout, you can instantiate the View object in code and start building your tree, but the easiest and most efficient way to define your layout is to use an XML file. Using XML to construct the layout is more human-friendly. Reading habits, while XML is similar to HTML Use the name of the XML element to represent a View. So the < TextView > element will create a TextView control in your interface, and a < LinearLayout > will create a LinearLayout container! For example, a simple vertical layout with a text view and a button would look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android :layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android: layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
<Button android:id="@+id/button"
android: When the App loads the above layout resources, Android will instantiate each node in the layout into an object. Then you can define some additional behaviors for these, query the status of the object, or modify the layout. For a complete guide to creating UI layouts, please refer to XML Layouts
User Interface Components
You don't need to use all View and ViewGroup objects to create your UI layout. Android provides us with some app controls and standard UI layout, you only need to define the content. These UI components have API documents introducing their properties, such as action bars, dialog boxes, status notification bars, etc.
Summary of this section
Okay, the translation may be a bit difficult to pronounce. Hey, I’m an English blind person and I’m trying my best. Let’s briefly summarize the above content:
The graphical interface in Android is composed of View, ViewGroup and their subclasses: View: The parent class of all visual controls, providing component rendering and time processing methods ViewGroup: View A subclass of a class can have child controls and can be regarded as a container The controls in the Android UI are stacked according to this hierarchical tree structure, and there are two ways to create a UI layout: Write the code yourself in Java or define the layout through XML, the latter is more convenient and easier to understand! It is also our most commonly used method! In addition, we rarely use View and ViewGroup directly to write layouts. More It's time to use their subclass controls or containers to build layouts!
Well, just have a general understanding of View and ViewGroup. We usually don’t use them directly, usually when customizing Views. Only then can I use these two things!