1 page.title=UI Overview 2 @jd:body 3 4 5 <p>All user interface elements in an Android app are built using {@link android.view.View} and 6 {@link android.view.ViewGroup} objects. A {@link android.view.View} is an object that draws 7 something on the screen that the user can interact with. A {@link android.view.ViewGroup} is an 8 object that holds other {@link android.view.View} (and {@link android.view.ViewGroup}) objects in 9 order to define the layout of the interface.</p> 10 11 <p>Android provides a collection of both {@link android.view.View} and {@link 12 android.view.ViewGroup} subclasses that offer you common input controls (such as buttons and text 13 fields) and various layout models (such as a linear or relative layout).</p> 14 15 16 <h2 id="Layout">User Interface Layout</h2> 17 18 <p>The user interface for each component of your app is defined using a hierarchy of {@link 19 android.view.View} and {@link android.view.ViewGroup} objects, as shown in figure 1. Each view group 20 is an invisible container that organizes child views, while the child views may be input 21 controls or other widgets that 22 draw some part of the UI. This hierarchy tree can be as simple or complex as you need 23 it to be (but simplicity is best for performance).</p> 24 25 <img src="{@docRoot}images/viewgroup.png" alt="" /> 26 <p class="img-caption"><strong>Figure 1.</strong> Illustration of a view hierarchy, which defines a 27 UI layout.</p> 28 29 <p>To declare your layout, you can instantiate {@link android.view.View} objects in code and start 30 building a tree, but the easiest and most effective way to define your layout is with an XML file. 31 XML offers a human-readable structure for the layout, similar to HTML.</p> 32 33 <p>The name of an XML element for a view is respective to the Android class it represents. So a 34 <code><TextView></code> element creates a {@link android.widget.TextView} widget in your UI, 35 and a <code><LinearLayout></code> element creates a {@link android.widget.LinearLayout} view 36 group. </p> 37 38 <p>For example, a simple vertical layout with a text view and a button looks like this:</p> 39 <pre> 40 <?xml version="1.0" encoding="utf-8"?> 41 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 42 android:layout_width="fill_parent" 43 android:layout_height="fill_parent" 44 android:orientation="vertical" > 45 <TextView android:id="@+id/text" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:text="I am a TextView" /> 49 <Button android:id="@+id/button" 50 android:layout_width="wrap_content" 51 android:layout_height="wrap_content" 52 android:text="I am a Button" /> 53 </LinearLayout> 54 </pre> 55 56 <p>When you load a layout resource in your app, Android initializes each node of the layout into a 57 runtime object you can use to define additional behaviors, query the object state, or modify the 58 layout.</p> 59 60 <p>For a complete guide to creating a UI layout, see <a href="declaring-layout.html">XML 61 Layouts</a>. 62 63 64 <h2 id="UIComponents">User Interface Components</h2> 65 66 <p>You don't have to build all of your UI using {@link android.view.View} and {@link 67 android.view.ViewGroup} objects. Android provides several app components that offer 68 a standard UI layout for which you simply need to define the content. These UI components each 69 have a unique set of APIs that are described in their respective documents, such as <a 70 href="{@docRoot}training/appbar/index.html">Adding the App Bar</a>, <a 71 href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a>, and <a 72 href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Notifications</a>.</p> 73 74 75