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: 
 <?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 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>
desc An 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>
 desc Includes a layout file into this layout.
layout Layout resource. Required. Reference to a layout resource.
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 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:

Value Description
match_parent Sets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_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 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:

 <?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 the onCreate() method:

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

Layout Resource官方教程(1)简介的更多相关文章

  1. Layout Resource官方教程(3)在layout中用include嵌入其它layout

    简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...

  2. Layout Resource官方教程(4)<include>与<merge>

    Re-using Layouts with <include/> THIS LESSON TEACHES YOU TO Create a Re-usable Layout Use the ...

  3. Layout Resource官方教程(2)用ViewStub引用的嵌入的layout可推迟加载

    Loading Views On Demand THIS LESSON TEACHES YOU TO Define a ViewStub Load the ViewStub Layout YOU SH ...

  4. ContentProvider官方教程(2)简介、Content URIs

    In this document Overview Accessing a provider Content URIs Content Provider Basics A content provid ...

  5. Intent官方教程(1)简介和作用

    Intents An Intent is a messaging object you can use to request an action from another app component. ...

  6. ActionBar官方教程(1)简介及各区域介绍

    Action Bar The action bar is a window feature that identifies the user location, and provides user a ...

  7. SwiftUI 官方教程

    SwiftUI 官方教程 完整中文教程及代码请查看 https://github.com/WillieWangWei/SwiftUI-Tutorials   SwiftUI 官方教程 SwiftUI ...

  8. Node.js 教程 01 - 简介、安装及配置

    系列目录: Node.js 教程 01 - 简介.安装及配置 Node.js 教程 02 - 经典的Hello World Node.js 教程 03 - 创建HTTP服务器 Node.js 教程 0 ...

  9. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

随机推荐

  1. 浅析foreach原理

    在日常开发工作中,我们发现很多对象都能通过foreach来遍历,比如HashTable.Dictionary.数组等数据类型.那为何这些对象能通过foreach来遍历呢?如果写一个普通的Person类 ...

  2. Invalid result location value/parameter

    Invalid result location value/parameter(struts2),该问题在myeclipse8.6一下的版本不会出现,但是在myeclipse9.0中就会出现该错误.有 ...

  3. 【html】【20】高级篇--轮播图[聚焦]

    下载: http://sc.chinaz.com/jiaoben/151204445580.htm 效果: html <!doctype html> <html> <he ...

  4. DataBase 总结开篇

    系列说明 本系列将总结(SQL)数据库技术在日常开发中引用,读者群体假设为三类:没接触过SQL的入门程序员.有过一两年经验的程序员.三年以上接触过性能调优的程序员.按照这个分类本系列大体分为三篇 第一 ...

  5. MVC异步 导入excel文件

    View页面 js文件.封装到一个js文件里面 (function ($) { //可以忽略 var defaultSettings = { url: "http://upload.zhtx ...

  6. 04_线程的创建和启动_使用Callable和Future的方式

    [简述] 从java5开始,java提供了Callable接口,这个接口可以是Runnable接口的增强版, Callable接口提供了一个call()方法作为线程执行体,call()方法比run() ...

  7. CUDA_矢量相加

    #include<iostream> #define N 10 _ _global_ _ void add(*a,*b,*c) { int tid=blockIdx.x; if(tid&l ...

  8. bzoj4637:期望

    思路:最小生成树计数只不过加了一个期望,由于期望具有线性性质,就可以转化为每条边的期望之和,那么一条边的期望如何求呢,在最小生成树记数中,是把相同边权的一起处理,之后把属于连通块内的点缩点,也就是说, ...

  9. 07_例子讲解:rlCoachKin + rlCoachMdl

    RL提供了2个相当简单的基于socket的虚拟化机器人,是socket服务端. rlCoachKin是用于D-H运动链(在rlkin\*xml中定义的). rlCoachMdl相当于任意几何的工具链. ...

  10. Java初始化理解与总结 转载

    Java的初始化可以分为两个部分: (a)类的初始化 (b)对象的创建 一.类的初始化 1.1 概念介绍: 一个类(class)要被使用必须经过装载,连接,初始化这样的过程. 在装载阶段,类装载器会把 ...