Layout Resource官方教程(1)简介
Layout Resource
SEE ALSO
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, aView, or a<merge>element, but there must be only one root element and it must contain thexmlns:androidattribute with theandroidnamespace as shown. - ELEMENTS:
| <ViewGroup> | |
| desc |
A container for other layout of the child elements in different ways. Different kinds of and implementations of the |
| android:id |
Resource ID. A unique resource name for the element, which you can use to obtain a reference to the application. See more about the value for |
| android:layout_height |
Dimension or keyword. Required. The height for the group, as a dimension value (or dimension resource) or a keyword ( |
| android:layout_width |
Dimension or keyword. Required. The width for the group, as a dimension value (or dimension resource) or a keyword ( |
| other |
More attributes are supported by the For a reference of all available attributes, see the corresponding reference documentation for the |
| <View> | |
| desc | An individual UI component, generally referred to as a "widget". Different kinds of View objects include TextView, Button, and CheckBox. |
| android:id |
Resource ID. A unique resource name for the element, which you can use to obtain a reference to the See more about the value for |
| android:layout_height |
Dimension or keyword. Required. The height for the element, as a dimension value (or dimension resource) or a keyword ( |
| android:layout_width |
Dimension or keyword. Required. The width for the element, as a dimension value (or dimension resource) or a keyword ( |
| other |
More attributes are supported by the 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 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 is also declared. |
| android:layout_width |
Dimension or keyword. Overrides the width given to the root view in the included layout. Only effective if also declared. |
|
You can include any other layout attributes in the will override those defined in the root element. Caution: If you want to override layout attributes using the
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 its |
|
| <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 layout in another layout file using |
|
-
Value for
android:idFor the ID value, you should usually use this syntax form:
"@+id/name". The plus symbol,+, indicates that this is a new resource ID and theaapttool will create a new resource integer in theR.javaclass, if it doesn't already exist. For example:<TextView android:id="@+id/nameTextbox"/>
The
nameTextboxname is now a resource ID attached to this element. You can then refer to theTextViewto which the ID is associated in Java:findViewById(R.id.nameTextbox);
This code returns the
TextViewobject.However, if you have already defined an ID resource (and it is not already used), then you can apply that ID to a
Viewelement by excluding the plus symbol in theandroid:idvalue.Value for
android:layout_heightandandroid: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_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
ViewandViewGroupelements 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 theonCreate()method: -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
Layout Resource官方教程(1)简介的更多相关文章
- Layout Resource官方教程(3)在layout中用include嵌入其它layout
简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...
- Layout Resource官方教程(4)<include>与<merge>
Re-using Layouts with <include/> THIS LESSON TEACHES YOU TO Create a Re-usable Layout Use the ...
- Layout Resource官方教程(2)用ViewStub引用的嵌入的layout可推迟加载
Loading Views On Demand THIS LESSON TEACHES YOU TO Define a ViewStub Load the ViewStub Layout YOU SH ...
- ContentProvider官方教程(2)简介、Content URIs
In this document Overview Accessing a provider Content URIs Content Provider Basics A content provid ...
- Intent官方教程(1)简介和作用
Intents An Intent is a messaging object you can use to request an action from another app component. ...
- ActionBar官方教程(1)简介及各区域介绍
Action Bar The action bar is a window feature that identifies the user location, and provides user a ...
- SwiftUI 官方教程
SwiftUI 官方教程 完整中文教程及代码请查看 https://github.com/WillieWangWei/SwiftUI-Tutorials SwiftUI 官方教程 SwiftUI ...
- Node.js 教程 01 - 简介、安装及配置
系列目录: Node.js 教程 01 - 简介.安装及配置 Node.js 教程 02 - 经典的Hello World Node.js 教程 03 - 创建HTTP服务器 Node.js 教程 0 ...
- Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译
本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
随机推荐
- Java编写ArrayBasic制作一个简单的酒店管理系统
听老师讲了一些ArrayBasic的一些知识,让制作一个酒店管理系统,要求:显示酒店所有房间列表,预订房间.... 经过老师的指导写了一个代码,如下: import java.util.Scanner ...
- ### C++总结-[类成员函数]
C++类中的常见函数. #@author: gr #@date: 2015-07-23 #@email: forgerui@gmail.com 一.constructor, copy construc ...
- 在Xcode6中找回失去的模板
[Add]2014.07.27 添加OC category.protocol模板 Xcode 6从beta 3开始在创建新项目窗口中移除了“Empty Application”,如下: 其他选项很不幸 ...
- (转)Quartz.NET管理类
最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下. public class QuartzManager<T> where T : class,IJob ...
- QT 常用设置
博文都写在了云笔记里面了,见谅,不想维护两个版本. QT 常用设置
- android开发之GenyMotion与intelliJ的配置
(注意:这是在你的电脑上安装了intelliJ和安卓SDK后才进行的工作,如果没有intelliJ和安卓SDK,请先安装以上两样东西) 号称史上最快乐的模拟器GenyMotion,试一下. 第一步:下 ...
- bzoj 1096: [ZJOI2007]仓库建设
dp是很好想的了,关键是数据太大,普通dp肯定超时,所以一定有用某种优化,dp优化也就那么几种,这道题用的是斜率优化,先写出普通的状态转移方程: dp[i] = min{ dp[j] + Σ ( p ...
- [翻译]log4net教程
原文:log4net Tutorial 一.基础: log4net分为三部分:配置.设置和调用.配置通常是在app.webconfig或web.config文件中:为了增加灵活性,我们也可以使用单独的 ...
- 使用AutoMapper
一.AutoMapper初探: [参考Using AutoMapper: Getting Started] 1.新建空的ASP.NET MVC项目. 2.在Models文件夹添加类: public c ...
- mysql---union和左连接的两倒面试题
第一道: 思路:无非是将hid与gid与t表中的tname关联起来.实质上是三表关联(m,t,t) 先将hid与tname关联起来,运用左连接 再将结果集与t表中的tname关联起来,使得gid与tn ...