Android 布局资源

来源:互联网 发布:公元纪年算法 编辑:程序博客网 时间:2024/05/22 15:01

Layout Resource

See also

  1. Declaring Layout

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 a 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 either aViewGroup, aView, or a<merge> element, but there must be onlyone root element and it must contain thexmlns:android attribute with theandroidnamespace as shown.

elements:
<ViewGroup>
A container for other View elements. There are many different kinds ofViewGroup objects and each one lets you specify the layout of the child elements in different ways. Different kinds ofViewGroup objects includeLinearLayout,RelativeLayout, andFrameLayout.

You should not assume that any derivation of ViewGroup will accept nestedViews. SomeViewGroups are implementations of theAdapterView class, which determines its children only from anAdapter.

attributes:

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

More attributes are supported by the ViewGroup base class, and many more are supported by each implementation ofViewGroup. For a reference of all available attributes, see the corresponding reference documentation for theViewGroup class(for example, theLinearLayout XMLattributes).

<View>
An individual UI component, generally referred to as a "widget". Different kinds ofView objects includeTextView,Button, andCheckBox.

attributes:

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

More attributes are supported by the View base class, and many more are supported by each implementation ofView. ReadDeclaring Layout for more information. For a reference of all available attributes, see the corresponding reference documentation (for example, theTextView XML attributes).

<requestFocus>
Any element representing a View object can include this empty element, which gives it's parent initial focus on the screen. You can have only one of these elements per file.
<include>
Includes 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 included layout.
android:layout_height
Dimension or keyword. Overrides the height given to the root view in theincluded layout. Only effective ifandroid:layout_width is also declared.
android:layout_width
Dimension or keyword. Overrides the width given to the root view in theincluded layout. Only effective ifandroid:layout_height is also declared.

You can include any other layout attributes in the <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 the layout dimensions,you must override bothandroid:layout_height andandroid:layout_width—you cannot override only the height or only the width.If you override only one, it will not take effect. (Other layout properties, such as weight,are still inherited from the source layout.)

Another way to include a layout is to use ViewStub. It is a lightweightView that consumes no layout space until you explicitly inflate it, at which point, it includes alayout file defined by its android:layout attribute. For more information about usingViewStub, readLayoutTricks: ViewStubs.

<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 the<merge> element. This is particularly useful when you plan to include this layoutin another layout file using<include> andthis layout doesn't require a different ViewGroup container. For moreinformation about merging layouts, readLayoutTricks: Merging.

Value for android:id

For the ID value, you should usually use this syntax form: "@+id/name". Theplus symbol,+, indicates that this is a new resource ID and theaapt tool willcreate a new resource integer in theR.java class, if it doesn't already exist. Forexample:

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

The nameTextbox name is now a resource ID attached to this element. You can thenrefer to theTextView to which the ID is associated in Java:

findViewById(R.id.nameTextbox);

This code returns the TextView object.

However, if you have already defined an ID resource (and it is notalready used), then you can apply that ID to a View element by excluding theplus symbol in theandroid:id value.

Value for android:layout_height andandroid: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:

ValueDescriptionmatch_parentSets the dimension to match that of the parent element. Added in API Level 8 todeprecatefill_parent.fill_parentSets the dimension to match that of the parent element.wrap_contentSets the dimension only to the size required to fit the content of this element.

Custom View elements

You can create your own custom View andViewGroupelements 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,readBuilding Custom Components.

example:
XML file saved at 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 for an Activity, in theonCreate() method:

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

原创粉丝点击