布局资源在UI的活动或组件中定义UI的体系结构。html
file location:(文件位置)java
res/layout/filename.xml
文件名将用做资源IDandroid
编制资源数据类型:
指向视图(或子类)资源的资源指针。ide
资源引用:
In Java: R.layout.filename
In XML: @[package:]layout/filename
工具
语法:布局
<?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" | "match_parent" | "wrap_content"] android:layout_width=["dimension" | "match_parent" | "wrap_content"] [ViewGroup-specific attributes] > <View android:id="@[+][package:]id/resource_name" android:layout_height=["dimension" | "match_parent" | "wrap_content"] android:layout_width=["dimension" | "match_parent" | "wrap_content"] [View-specific attributes] > <requestFocus/> </View> <ViewGroup > <View /> </ViewGroup> <include layout="@layout/layout_resource"/> </ViewGroup>
原理:ui
<ViewGroup>this
其余视图元素的容器。有许多不一样种类的ViewGroup对象,每一种都容许您以不一样的方式指定子元素的布局。不一样类型的ViewGroup对象包括 LinearLayout、RelativeLayout和FrameLayout。 google
您不该该假定ViewGroup的任何派生都将接受嵌套视图。有些视图组是AdapterView类的实现,该类仅从适配器肯定其子类。spa
<View>
属性:
android:id
资源ID。元素的惟一资源名,您可使用它从应用程序中得到对ViewGroup的引用。有关android:id的更多信息,请参见下面。
android:layout_height
维度或关键字。必需的。元素的高度,做为维度值(或维度资源)或关键字(“match_parent”或“wrap_content”)。请参阅下面的有效值。
android:layout_width
维度或关键字。必需的。元素的宽度,做为维度值(或维度资源)或关键字(“match_parent”或“wrap_content”)。请参阅下面的有效值。
视图基类支持更多的属性,视图的每一个实现支持更多的属性。阅读布局以得到更多信息。有关全部可用属性的引用,请参见相应的引用文档(例如TextView XML属性)。
<requestFocus>
一个单独的UI组件,一般称为“小部件”。不一样类型的视图对象包括TextView、Button和复选框。
<include>
将布局文件包含到此布局中。
属性:
layout:
布局资源。必需的。引用布局资源。
android:id
资源ID。覆盖包含布局中给定给根视图的ID。
android:layout_height
维度或关键字。覆盖所包含布局中给定给根视图的高度。只有在同时声明android:layout_width时才有效。
android:layout_width
维度或关键字。覆盖所包含布局中给定给根视图的宽度。只有在同时声明android:layout_height时才有效。
您能够在<include>中包含根元素支持的任何其余布局属性,而且它们将覆盖根元素中定义的那些属性。
注意:若是您想使用<include>标签覆盖布局属性,则必须同时覆盖android:layout_height和android:layout_width,以便其余布局属性生效。
包含布局的另外一种方法是使用ViewStub。它是一个轻量级视图,在显式膨胀以前不消耗任何布局空间,此时,它包含一个由其android:layout属性定义的 布局文件。有关使用ViewStub的更多信息,请阅读按需加载视图。
<merge>
布局层次结构中没有绘制的替代根元素。当您知道这个布局将被放置到一个已经包含适当父视图的布局中,以包含<merge>元素的子元素时,将它用做根元素很是有用。当您计划使用<include>将该布局包含在另外一个布局文件中,而且该布局不须要不一样的ViewGroup容器时,这尤为有用。有关合并布局的更多信息,请阅读使用<include/>重用布局。
Value for android:id
连接
对于ID值,一般应该使用这种语法形式:“@+ ID /name”。加号+表示这是一个新的资源ID,若是不存在,aapt工具将在R.java类中建立一个新的资源整数。例如:
<TextView android:id="@+id/nameTextbox"/>
nameTextbox名称如今是附加到该元素的资源ID。而后,您能够参考在Java中与ID关联的TextView:
TextView textView = findViewById(R.id.nameTextbox);
这段代码返回TextView对象。
可是,若是您已经定义了一个ID资源(而且它尚未被使用),那么您能够经过在android: ID值中排除加号来将该ID应用于视图元素。
android:layout_height
and android:layout_width
连接高度和宽度值能够用Android支持的任意尺寸单位(px、dp、sp、pt、in、mm)表示,也能够用如下关键词表示:
Value | Description |
---|---|
match_parent |
Sets the dimension to match that of the parent element. Added in API Level 8 to deprecate 设置与父元素匹配的维度。在API级别8中添加,以反对fill_parent. |
wrap_content |
Sets the dimension only to the size required to fit the content of this element. 仅将维度设置为适合此元素内容所需的大小。 |
Custom View elements 连接
您能够建立本身的自定义视图和ViewGroup元素,并将它们应用到与标准布局元素相同的布局中。还能够指定XML元素中支持的属性。要了解更多信息,请参阅自定义组件开发人员指南。
例如:
XML文件保存在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="match_parent" android:layout_height="match_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>
这个应用程序代码将在onCreate()方法中加载一个活动的布局:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
另请参阅: