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:android
attribute with theandroid
namespace as shown.
ELEMENTS:
-
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 theaapt
tool will create a new resource integer in theR.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 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 not already used), then you can apply that ID to a
View
element by excluding the plus 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:
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
andViewGroup
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 theonCreate()
method: -
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
SEE ALSO:
-
一个布局资源定义了一个活动的用户界面或用户界面的组件体系结构。
-
文件位置:
-
水库/布局/ 文件名 的.xml
文件名 将被用作资源ID。
编译的资源数据类型:
-
资源指针
视图
(或子类)的资源。
资源引用:
-
在Java:
。R.layout 名
在XML:@ [ 包:]布局/ 文件名
句法:
-
<?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 >
注:根元素可以是一个
ViewGroup中
,一个视图
,或<合并>
元素,但只能有一个根元素,它必须包含的xmlns:android的
属性与Android的
命名空间,如图所示。
内容:
-
对于值
机器人:ID
对于ID值,你通常应该使用这种语法形式:
“@ + ID / 名称 ”
。加号,+
,表示这是一个新的资源ID和AAPT
工具将在创建新的资源整数R.java
类,如果它不存在。例如:<TextView的 机器人:ID = “@ + ID / nameTextbox” />
该
nameTextbox
名称现在是连接到该元素的资源ID。然后,您可以参考的TextView
到的ID是用Java有关:findViewById (ř 。ID 。nameTextbox );
此代码返回
TextView的
对象。但是,如果您已经定义了一个ID资源(和尚未使用),那么你可以申请该ID来一个
视图
排除在加符号元素ID:Android的
价值。对于值
layout_height:机器人
和机器人:layout_width
:高度和宽度值可以使用任何的表示 标注单位 Android支持(PX,DP,SP,PT,在,毫米)或以下关键字:
值 描述 match_parent
设置维度匹配的父元素。在API级别8弃用 FILL_PARENT
。FILL_PARENT
设置维度匹配的父元素。 WRAP_CONTENT
设置尺寸仅适合该元件的内容所需的尺寸。 自定义视图元素
您可以创建自己的自定义
视图
和一个ViewGroup
元素,并将它们应用到你的布局一样的标准布局元素。您还可以指定在XML元素支持的属性。要了解更多信息,请参阅自定义组件开发人员指南。
例:
-
在保存XML文件
RES /布局/ main_activity.xml
:<?xml的 我是一个 我是一个按钮“ /> </ LinearLayout中>
此应用程序代码将加载布局的
活动
,在的onCreate()
方法: -
public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . main_activity ); }
也可以看看:
-