Overview of the user interface
The user interface elements of all Android applications are built with view and ViewGroup objects. View is an object that is depicted on the phone screen to interact with the user. ViewGroup is a container object that includes the View object and other ViewGroup objects to define the layout of the included interfaces.
Android provides a collection of view and ViewGroup subclasses that provide us with frequently used input controls (such as button and text fields) and various layout models (for example, linear layout and relative layout)
Layout of the user interface
The user interface for each component of the application is defined by the hierarchy of the view and ViewGroup objects, as seen in 1. Each viewgroup is an invisible container used to organize the View object. These view objects may be input controls or other widgets. This hierarchical tree can be simple and complex depending on your needs (but simplicity is best for performance)
Figure 1. Illustrations that define the view hierarchy of the UI layout
After declaring a layout (called Setcontentview (R.layout.main) in the Activity class OnCreate method), you can instantiate a view pair in your code
Like and create a tree structure, the simplest and most efficient way is to define the layout in an XML file. This XML file provides a readable layout structure, similar to the HTML
The name of the corresponding XML element for view corresponds to the Android class it represents. So a <TextView> element creates a TextView part in the UI, and a <LinearLayout> element creates a LinearLayout view group
For example, a simple vertical layout that includes the text view and button is as follows:
<?XML version="1.0"encoding="Utf-8"?>
<linearlayoutxmlns:android="http://schemas.android.com/apk/res/android"
Android:layout_width="Fill_parent"
Android:layout_height="Fill_parent"
android:orientation="Vertical">
<textviewAndroid:id="@+id/text"
Android:layout_width="Wrap_content"
Android:layout_height="Wrap_content"
Android:text="I am a TextView"/>
<buttonAndroid:id="@+id/button"
Android:layout_width="Wrap_content"
Android:layout_height="Wrap_content"
Android:text="I am a button"/>
</LinearLayout>
When you load a layout resource into your application, Android initializes each node of the layout resource file to the object at execution time. Can get this object (Findviewbyid (r.id.tv)) to define additional behavior, query the state of this object, or change the layout.
Components of the user interface
You do not necessarily have to create the UI through the view and ViewGroup objects. Several of the application components provided by Android provide us with a standard user interface layout that we just need to define for this layout. These user interface components (for example, action Bar,dialogs,status notifications) have their own unique set of APIs, which are defined in their respective documents.