布局资源文件
原文链接:点击打开链接
一个布局资源文件是定义了一个Activity的UI 或者一个组件的框架。
文件地址:
res/layout/filename.xml
filename 被用作资源这个资源的ID。
编译出的资源类型:
资源被指向一个 View(或子类)。
资源引用:
Java: R.layout.filename
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" | "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,也可以是
View , 也可以是一个
<merge>元素 ,但是必须只有一个根元素,必须以
xmlns:android开头用来表示android 命名空间。
元素:
<ViewGroup>
一个容器,可以包含其他元素,有许多不同的ViewGroup 对象,每一个都允许你用不同的方式定义它们里面子元素的布局。不同ViewGroup对象有:LinearLayout,
RelativeLayout, andFrameLayout.
你应该知道,ViewGroup不会嵌套任何的views。 一些ViewGroups 是实现了 dapterView类,它只允许它的子视图来自于Adapter. 。
属性:
android:id
资源ID。唯一的资源明标志,可以使用它作为这个ViewGroup 的引用。更多内容请参照value forandroid:id 。
android:layout_height
表示这个group的高度,必须是一个尺寸值(或 dimension resource) 或者关键字("fill_parent"or "wrap_content")。
android:layout_witdh
表示这个group的宽度,必须是一个尺寸值(或 dimension resource) 或者关键字("fill_parent"or "wrap_content")。
若要获取更多属性支持,请参考ViewGroup 基类,或者ViewGroup的实现子类。为了获取有效的属性,请参照相关的实现类(如LinearLayout XMLattributes)。
<View>
一个单独的UI组件,一般被称为“widget” 。 不同的View 对象包括TextView,Button, andCheckBox.。
属性:
android:id
资源ID。唯一资源名字标识符,你可以使用它在你的应用里做引用这个View 。
android:layout_height
表示这个group的高度,必须是一个尺寸值(或 dimension resource) 或者关键字("fill_parent"or "wrap_content")。
android:layout_witdh
表示这个group的宽度,必须是一个尺寸值(或 dimension resource) 或者关键字("fill_parent"or "wrap_content")。
更多的属性请参照View. 基类,或者该类的实现,阅读Layouts获得更多消息。获取一个全部有效的属性,请参考对应的文档(比如,TextView XML attributes)。
<requestFocus>
任何一个view的对象都可以包含这个空元素,它指定了它的父类在屏幕时候初始的聚焦。在每个文件里面,只能有一个。
<include>
将一个layout文件内嵌到该布局文件里面。
属性:
layout
布局资源的引用。
android:id
资源ID,覆盖掉源layout里面的ID。
android:layout_height
尺寸或者关键字,覆盖源layout里面的相关信息。
android:layout_width
尺寸或者关键字,覆盖源layout里面相关信息。
你可以包含其他属性,源layout里面支持的。
注意:在使用<include>标志时候,你必须将 layout_height 和layout_width都覆盖,这样,其他属性才会有效。
另外一种包含一个布局的方式是使用 ViewStub,它是一个灵巧的View,不包含布局空间,只有你 inflate它,基于这一点,它包含一个android:layout属性 。更多信息,请参照:ViewStub, Loading Views On Demand 。
<merge>
另一种在布局层次中未绘制的根元素。一般使用在替换掉一个子元素的情况,可以直接将<merge>里面元素替换到根布局里面,请参照:Layouts with <include/>.
自定义视图元素。
你可以创建自己的 View和 ViewGroup元素,将它们应用到你的布局里面。你可以制定在XML 元素的属性,请参阅Custom Components。
例子:
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="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>
应用代码将会加载布局到一个Activity里面,在onCreate() 方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
本文详细介绍了Android中的布局资源文件,包括文件位置、编译资源类型、引用方式及XML语法等。解析了各种元素如ViewGroup、View的功能与属性,并通过实例展示了如何创建一个简单的布局。
3581

被折叠的 条评论
为什么被折叠?



