Layout Resource官方教程(1)简介

Layout Resource

SEE ALSO

  1. Layouts

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: 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@[+][package:]id/resource_name"
 4     android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
 5     android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
 6     [ViewGroup-specific attributes] >
 7     <View
 8         android:id="@[+][package:]id/resource_name"
 9         android:layout_height=["dimension" | "fill_parent" | "wrap_content"]
10         android:layout_width=["dimension" | "fill_parent" | "wrap_content"]
11         [View-specific attributes] >
12         <requestFocus/>
13     </View>
14     <ViewGroup >
15         <View />
16     </ViewGroup>
17     <include layout="@layout/layout_resource"/>
18 </ViewGroup>

Note: The root element can be either a ViewGroup, a View, or a <merge> element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown.

ELEMENTS:
<ViewGroup>
desc

A container for other View elements. There are many different kinds of ViewGroup objects and each one lets you specify the

layout of the child elements in different ways. Different kinds of ViewGroup objects include LinearLayoutRelativeLayout,

and  FrameLayout. You should not assume that any derivation of ViewGroup will accept nested Views. Some ViewGroups are

implementations of the  AdapterView class, which determines its children only from an Adapter.

android:id

Resource ID. A unique resource name for the element, which you can use to obtain a reference to the ViewGroup from your

application. See more about the value for android:id below.

android:layout_height

Dimension or keyword. Required. The height for the group, as a 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 group, as a dimension value (or dimension resource) or a keyword

("fill_parent"  or "wrap_content"). See the valid values below.

other

More attributes are supported by the ViewGroup base class, and many more are supported by each implementation of ViewGroup.

For a reference of all available attributes, see the corresponding reference documentation for the ViewGroup class (for example,

the LinearLayout XML attributes).

<View>
descAn individual UI component, generally referred to as a "widget". Different kinds of View objects include TextViewButton, and CheckBox.
android:id

Resource ID. A unique resource name for the element, which you can use to obtain a reference to the View from your application.

See more about the value for android:id below.

android:layout_height

Dimension or keyword. Required. The height for the element, as a 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, as a dimension value (or dimension resource) or a keyword

("fill_parent" or "wrap_content"). See the valid values below.

other

More attributes are supported by the View base class, and many more are supported by each implementation of View.

Read Layouts for more information. For a reference of all available attributes, see the corresponding reference

documentation (for example, the TextView XML attributes).

<requestFocus>
 

Any element representing a View object 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>
 descIncludes a layout file into this layout.
layoutLayout resource. Required. Reference to a layout resource.
android:idResource 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 the included layout. Only effective if android:layout_width 

is also declared.

android:layout_width

Dimension or keyword. Overrides the width given to the root view in the included layout. Only effective if android:layout_height is

also declared.

 

You can include any other layout attributes in the <include> that are supported by the root element in the included layout and they

will override those defined in the root element.

Caution: If you want to override layout attributes using the <include> tag, you must override bothandroid:layout_height and 

android:layout_width in order for other layout attributes to take effect. Another way to include a layout is to use ViewStub.

It is a lightweight View that consumes no layout space until you explicitly inflate it, at which point, it includes a layout file defined by

itsandroid:layout attribute. For more information about using ViewStub, read Loading Views On Demand.

<merge>

  An alternative root element that is not drawn in the layout hierarchy. Using this as the root element is useful when you know that this layout will be placed into

a layout that already contains the appropriate parent View to contain the children of the <merge> element. This is particularly useful when you plan to include this

layout in another layout file using <include> and this layout doesn't require a different ViewGroup container. For more information about merging layouts, read 

Re-using Layouts with <include/>.


Value for android:id

  For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist. For example:

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

  The nameTextbox name is now a resource ID attached to this element. You can then refer to the TextViewto 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 not already used), then you can apply that ID to a View element by excluding the plus symbol in the android:id value.

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

ValueDescription
match_parentSets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_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 and ViewGroup elements and apply them to your layout the same as a standard layout element. You can also specify the attributes supported in the XML element. To learn more, see the Custom Components developer guide.

EXAMPLE:
  XML file saved at  res/layout/main_activity.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3               android:layout_width="fill_parent" 
 4               android:layout_height="fill_parent" 
 5               android:orientation="vertical" >
 6     <TextView android:id="@+id/text"
 7               android:layout_width="wrap_content"
 8               android:layout_height="wrap_content"
 9               android:text="Hello, I am a TextView" />
10     <Button android:id="@+id/button"
11             android:layout_width="wrap_content"
12             android:layout_height="wrap_content"
13             android:text="Hello, I am a Button" />
14 </LinearLayout>

This application code will load the layout for an Activity, in the onCreate() method:

1 public void onCreate(Bundle savedInstanceState) {
2     super.onCreate(savedInstanceState);
3     setContentView(R.layout.main_activity);
4 }

 


 

转载于:https://www.cnblogs.com/sjjg/p/4905775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值