From f246b0c4e972635a62693e6aca07911c33c10ccf Mon Sep 17 00:00:00 2001
From: Ricardo Cervera See also
The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile. For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems. As such, this document focuses on teaching you how to declare your layout in XML. If you're -interested in instantiating View objects at runtime, refer to the {@link android.view.ViewGroup} and +interested in instantiating View objects at runtime, refer to the {@link android.view.ViewGroup} and {@link android.view.View} class references.
In general, the XML vocabulary for declaring UI elements closely follows the structure and naming of the classes and methods, where element names correspond to class names and attribute names correspond to methods. In fact, the correspondence is often so direct that you can guess what XML attribute corresponds to a class method, or guess what class corresponds to a given XML element. However, note that not all vocabulary is identical. In some cases, there are slight naming differences. For @@ -102,7 +102,7 @@ to hold a {@link android.widget.TextView} and a {@link android.widget.Button}: </LinearLayout> -
After you've declared your layout in XML, save the file with the .xml extension,
+
After you've declared your layout in XML, save the file with the .xml extension,
in your Android project's res/layout/ directory, so it will properly compile.
More information about the syntax for a layout XML file is available in the Layout ResourcesLoad the XML Resource
When you compile your application, each XML layout file is compiled into a
-{@link android.view.View} resource. You should load the layout resource from your application code, in your
+{@link android.view.View} resource. You should load the layout resource from your application code, in your
{@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()} callback implementation.
-Do so by calling {@link android.app.Activity#setContentView(int) setContentView()},
-passing it the reference to your layout resource in the form of:
-R.layout.layout_file_name.
+Do so by calling {@link android.app.Activity#setContentView(int) setContentView()},
+passing it the reference to your layout resource in the form of:
+R.layout.layout_file_name.
For example, if your XML layout is saved as main_layout.xml, you would load it
for your Activity like so:
@@ -126,7 +126,7 @@ public void onCreate(Bundle savedInstanceState) {
The onCreate() callback method in your Activity is called by the Android framework when
-your Activity is launched (see the discussion about lifecycles, in the
+your Activity is launched (see the discussion about lifecycles, in the
Activities
document).
Every View and ViewGroup object supports their own variety of XML attributes.
Some attributes are specific to a View object (for example, TextView supports the textSize
attribute), but these attributes are also inherited by any View objects that may extend this class.
-Some are common to all View objects, because they are inherited from the root View class (like
-the id attribute). And, other attributes are considered "layout parameters," which are
+Some are common to all View objects, because they are inherited from the root View class (like
+the id attribute). And, other attributes are considered "layout parameters," which are
attributes that describe certain layout orientations of the View object, as defined by that object's
parent ViewGroup object.
Any View object may have an integer ID associated with it, to uniquely identify the View within the tree.
-When the application is compiled, this ID is referenced as an integer, but the ID is typically
+When the application is compiled, this ID is referenced as an integer, but the ID is typically
assigned in the layout XML file as a string, in the id attribute.
This is an XML attribute common to all View objects
-(defined by the {@link android.view.View} class) and you will use it very often.
+(defined by the {@link android.view.View} class) and you will use it very often.
The syntax for an ID, inside an XML tag is:
android:id="@+id/my_button"@@ -170,7 +170,7 @@ resources class, rather than the local resources class. android:text="@string/my_button_text"/> -
{@link android.app.Activity#onCreate(Bundle) onCreate()} method):
Button myButton = (Button) findViewById(R.id.my_button); @@ -178,16 +178,16 @@ Button myButton = (Button) findViewById(R.id.my_button);
Defining IDs for view objects is important when creating a {@link android.widget.RelativeLayout}. -In a relative layout, sibling views can define their layout relative to another sibling view, +In a relative layout, sibling views can define their layout relative to another sibling view, which is referenced by the unique ID.
An ID need not be unique throughout the entire tree, but it should be -unique within the part of the tree you are searching (which may often be the entire tree, so it's best +unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).
XML layout attributes named layout_something define
+
XML layout attributes named layout_something define
layout parameters for the View that are appropriate for the ViewGroup in which it resides.
Every ViewGroup class implements a nested class that extends {@link @@ -201,7 +201,7 @@ view group defines layout parameters for each child view (including the child vi parameters associated with each view.
Note that every LayoutParams subclass has its own syntax for setting -values. Each child element must define LayoutParams that are appropriate for its parent, +values. Each child element must define LayoutParams that are appropriate for its parent, though it may also define different LayoutParams for its own children.
All view groups include a width and height (layout_width and
@@ -236,7 +236,7 @@ Available Resources document.
It is possible to retrieve the location of a view by invoking the methods {@link android.view.View#getLeft()} and {@link android.view.View#getTop()}. The former returns the left, or X, @@ -246,7 +246,7 @@ Available Resources document.
whengetLeft() returns 20, that means the view is located 20 pixels to the
right of the left edge of its direct parent.
-
+
In addition, several convenience methods are offered to avoid unnecessary computations, namely {@link android.view.View#getRight()} and {@link android.view.View#getBottom()}. @@ -254,14 +254,14 @@ Available Resources document.
rectangle representing the view. For instance, calling {@link android.view.View#getRight()} is similar to the following computation:getLeft() + getWidth().
-
+
The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.
- +The first pair is known as measured width and measured height. These dimensions define how big a view wants to be @@ -269,16 +269,16 @@ Available Resources document.
measured dimensions can be obtained by calling {@link android.view.View#getMeasuredWidth()} and {@link android.view.View#getMeasuredHeight()}. - +The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling - {@link android.view.View#getWidth()} and {@link android.view.View#getHeight()}. + {@link android.view.View#getWidth()} and {@link android.view.View#getHeight()}.
- +To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. @@ -287,9 +287,9 @@ Available Resources document.
2 pixels to the right of the left edge. Padding can be set using the {@link android.view.View#setPadding(int, int, int, int)} method and queried by calling {@link android.view.View#getPaddingLeft()}, {@link android.view.View#getPaddingTop()}, - {@link android.view.View#getPaddingRight()} and {@link android.view.View#getPaddingBottom()}. + {@link android.view.View#getPaddingRight()} and {@link android.view.View#getPaddingBottom()}. - +Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support. Refer to @@ -297,13 +297,13 @@ Available Resources document.
{@link android.view.ViewGroup.MarginLayoutParams} for further information. -For more information about dimensions, see +
For more information about dimensions, see Dimension Values.
- - - + + +