前言
声明
欢迎转载,但请保留文章原始出处:)
resources types - layout
译者署名: 呆呆大虾
版本:android 3.2 r1
原文
<a href="http://developer.android.com/guide/topics/resources/layout-resource.html">http://developer.android.com/guide/topics/resources/layout-resource.html</a>
参见
布局资源
布局(layout)资源用于定义activity内用户界面(ui)或者用户界面控件的布局结构。
文件位置:
res/layout/filename.xml
文件名filename将作为资源id。
编译后资源的数据类型:
资源引用:
java代码:r.layout.filename
xml代码:@[package:]layout/filename
语法:
<?xml version="1.0" encoding="utf-8"?>
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>
元素:
属性:
android:id
android:layout_height
android:layout_width
将另一个布局(layout)文件包含进来。
layout
layout资源。必填项。引用布局资源。
资源id。覆盖包含进来的layout资源中的根view id。
度量或关键字。覆盖包含进来的layout资源中根view给出的高度。仅在同时给出android:layout_width时才生效。
度量或关键字。覆盖包含进来的layout资源中根view给出的高度。仅在同时给出android:layout_height时才生效。
只要是被包含的layout资源根元素支持的属性,都能在<include>元素中包含进来,并且会覆盖本资源内根元素已定义的属性。
注意: 如果要覆盖layout的度量(长度和宽度),必须同时覆盖android:layout_height和 android:layout_width——不能只覆盖长度或只覆盖宽度。如果只覆盖其中一个,则不会生效。(其他布局属性,比如weight,仍然继承自原有的layout。)
对于id的值,通常应该用"@+id/name"的语法格式。加号+表示这是个新的资源id。如果r.java类中不存在此资源的话,aapt工具将在r.java类中生成一个新的resource整数标识。例如:
<textview android:id="@+id/nametextbox"/>
findviewbyid(r.id.nametextbox) ;
android:layout_height 和 android:layout_width的值
值
描述
match_parent
和父元素匹配(充满)。自api level 8开始加入,以淘汰fill_parent。
fill_parent
和父元素匹配(充满)。
wrap_content
仅本元素内容需要的大小。
自定义view元素
示例:
存放在res/layout/main_activity.xml的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:text="hello, i am a button" />
</linearlayout>
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview.(r.layout.main_activity);
}
转载:http://www.cnblogs.com/over140/archive/2011/10/11/2207643.html