A layout defines
the visual structure for a user interface, such as the UI for an
activity
or app
widget. You can declare a layout in two ways:
Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements.
Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you've defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout.
Example:
XML layout that uses a vertical
<?xml version="1.0" encoding="utf-8"?>
<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="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
After you've declared your layout in XML, save the file with the
Load the XML Resource:
When you compile your application, each XML layout file is compiled into a
For example, if your XML layout is saved as
This information from Android developers site(for more information):-http://developer.android.com/guide/topics/ui/declaring-layout.html#load
An Android layout is a class that handles arranging the way its children appear on the screen. Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts. You could also create your own custom layout by making a class that inherits from ViewGroup.
The standard Layouts are:
AbsoluteLayout
FrameLayout
LinearLayout
RelativeLayout
TableLayout
- Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.
- Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.
Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements.
Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you've defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout.
Example:
XML layout that uses a vertical
LinearLayout
to hold a TextView
and a Button
:<?xml version="1.0" encoding="utf-8"?>
<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="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
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. Load the XML Resource:
When you compile your application, each XML layout file is compiled into a
View
resource. You should load the layout resource from your application
code, in your Activity.onCreate()
callback implementation. Do so by calling 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:public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); }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 Activities
document).This information from Android developers site(for more information):-http://developer.android.com/guide/topics/ui/declaring-layout.html#load
An Android layout is a class that handles arranging the way its children appear on the screen. Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts. You could also create your own custom layout by making a class that inherits from ViewGroup.
The standard Layouts are:
AbsoluteLayout
FrameLayout
LinearLayout
RelativeLayout
TableLayout
No comments:
Post a Comment