Official Android documentation: (3) Resource-3.6.5 Layout

Source: Internet
Author: User

A layout resource defines the architecture for the UI in an Activity or a component of a UI.

File location:
res/layout/filename.xml
The filename will be used as the resource ID.
Compiled resource datatype:
Resource pointer to View(Or subclass) resource.
Resource reference:
In Java: R.layout.filename
In XML: @[package:]layout/filename
Syntax:
<?xml version="1.0" encoding="utf-8"?><ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@[+][package:]id/resource_name"    android:layout_height=["dimension" | "fill_parent" | "wrap_content"]    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]    [ViewGroup-specific attributes] >    <View        android:id="@[+][package:]id/resource_name"        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]        [View-specific attributes] >        <requestFocus/>    </View>    <ViewGroup >        <View />    </ViewGroup>    <include layout="@layout/layout_resource"/></ViewGroup>

Note:The root element can be eitherViewGroup,View, Or<merge>Element, but there must be onlyone root element and it must containxmlns:androidAttribute withandroidNamespace as shown.

Elements:
<ViewGroup>
A container for other ViewElements. There are unsupported different kinds ViewGroupObjects and each one lets you specify the layout of the child elements in different ways. Different kinds ViewGroupObjects include LinearLayout, RelativeLayout, And FrameLayout.

You shoshould not assume that any derivationViewGroupWill accept nestedViewS. SomeViewGroupS are implementations ofAdapterViewClass, which determines its children only fromAdapter.

Attributes:

android:id
Resource ID. A unique resource name for the element, which you canuse to obtain a reference to ViewGroupFrom your application. See moreabout the value android:idBelow.
android:layout_height
Dimension or keyword. Required. The height for the group, as adimension value (or dimension resource) or a keyword ( "fill_parent"Or "wrap_content"). See the valid values below.
android:layout_width
Dimension or keyword. Required. The width for the group, as adimension value (or dimension resource) or a keyword ( "fill_parent"Or "wrap_content"). See the valid values below.

More attributes are supported byViewGroupBase class, and more are supported by each implementationViewGroup. For a reference of all available attributes, see the corresponding reference documentation forViewGroupClass (for example, the LinearLayout XMLattributes ).

<View>
An individual UI component, generally referred to as a "widget". Different kinds ViewObjects include TextView, Button, And CheckBox.

Attributes:

android:id
Resource ID. A unique resource name for the element, which you can use to obtain a reference to ViewFrom your application. See more aboutthe value android:idBelow.
android:layout_height
Dimension or keyword. Required. The height for the element, asa dimension value (or dimension resource) or a keyword ( "fill_parent"Or "wrap_content"). See the valid values below.
android:layout_width
Dimension or keyword. Required. The width for the element, asa dimension value (or dimension resource) or a keyword ( "fill_parent"Or "wrap_content"). See the valid values below.

More attributes are supported byViewBase class, and more are supported by each implementationView. Read Layouts for more information. Fora reference of all available attributes, see the corresponding reference documentation (for example, the TextView XML attributes ).

<requestFocus>
Any element representing ViewObject can include this empty element, which gives its parent initial focus on the screen. You can have only one of these elements per file.
<include>
Except des a layout file into this layout.

Attributes:

layout
Layout resource. Required. Reference to a layoutresource.
android:id
Resource ID. Overrides the ID given to the root view in the specified ded layout.
android:layout_height
Dimension or keyword. Overrides the height given to the root view in theincluded layout. Only valid tive if android:layout_widthIs also declared.
android:layout_width
Dimension or keyword. Overrides the width given to the root view in theincluded layout. Only valid tive if android:layout_heightIs also declared.

You can include any other layout attributes in<include>That aresupported by the root element in the included layout and they will override those defined in theroot element.

Caution:If you want to override layout attributes using<include>Tag, you must override bothandroid:layout_heightAndandroid:layout_widthIn order for other layout attributes to take effect.

Another way to include a layout is to useViewStub. It is a lightweightView that consumes no layout space until you explicitly inflate it, at which point, it should des alayout file defined by itsandroid:layoutAttribute. For more information about usingViewStub, Read Loading Views On Demand.

<merge>
An alternative root element that is not drawn in the layout hierarchy. using this as theroot element is useful when you know that this layout will be placed into a layoutthat already contains the appropriate parent View to contain the children of <merge>Element. This is Special useful when you plan to include this layoutin another layout file using <include>Andthis layout doesn' t require a different ViewGroupContainer. For moreinformation about merging layouts, read Re-using Layouts with <include/>.
Value android:id

For the ID value, you shoshould usually use this syntax form:"@+id/name". Theplus symbol,+, Indicates that this is a new resource ID andaaptTool willcreate a new resource integer inR.javaClass, if it doesn' t already exist. Forexample:

<TextView android:id="@+id/nameTextbox"/>

ThenameTextboxName is now a resource ID attached to this element. You can thenrefer toTextViewTo which the ID is associated in Java:

findViewById(R.id.nameTextbox);

This code returnsTextViewObject.

However, if you have already defined an ID resource (and it is notalready used), then you can apply that ID toViewElement by excluding theplus symbol inandroid:idValue.

Value android:layout_heightAnd android:layout_width:

The height and width value can be expressed using any of the dimension units supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:

Value Description
match_parent Sets the dimension to match that of the parent element. Added in API Level 8 todeprecatefill_parent.
fill_parent Sets the dimension to match that of the parent element.
wrap_content Sets the dimension only to the size required to fit the content of this element.
Custom View elements

You can create your own mViewAndViewGroupElements and apply them to your layout the same as a standard layoutelement. You can also specify the attributes supported in the XML element. To learn more, see the Custom Components release guide.

Example:
XML file saved res/layout/main_activity.xml:
<?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>

This application code will load the layout forActivity, InonCreate()Method:

public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main_activity);}
See also:
  • Layouts
  • View
  • ViewGroup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.