- android
- android.accessibilityservice
- android.accounts
- android.content
- android.database.sqlite
- android.graphics
- android.location
- android.media
- android.net
- android.os
- android.text
- android.view
- android.view.inputmethod
- android.widget
- AbsListView
- AbsoluteLayout
- AbsSeekBar
- AbsSpinner
- AdapterView
- AnalogClock
- BaseAdapter
- BaseExpandableListAdapter
- Button
- CheckBox
- CheckedTextView
- Checkable
- Chronometer
- CompoundButton
- CursorAdapter
- CursorTreeAdapter
- DatePicker
- DialerFilter
- DigitalClock
- EditText
- Filter
- Filter.FilterListener
- Filter.FilterResults
- ExpandableListAdapter
- Filterable
- Gallery
- Gallery.LayoutParams
- GridView
- GridLayout
- RadioGroup
- ImageView
- HorizontalScrollView
- ImageButton
- ImageSwitcher
- FilterQueryProvider
- ListAdapter
- ListView
- MediaController
- QuickContactBadge
- RadioButton
- RatingBar
- RelativeLayout
- RemoteViews
- ResourceCursorAdapter
- ResourceCursorTreeAdapter
- Scroller
- ScrollView
- SearchView
- SeekBar
- SeekBar.OnSeekBarChangeListener
- SimpleAdapter
- SimpleCursorAdapter
- SimpleCursorTreeAdapter
- SimpleExpandableListAdapter
- SlidingDrawer
- Spinner
- SpinnerAdapter
- WrapperListAdapter
- TabHost
- TabHost.TabSpec
- TextView
- TimePicker
- Toast
- TableLayout
- TableRow
- TableRow.LayoutParams
- TabWidget
- TextSwitcher
- ToggleButton
- TwoLineListItem
- VideoView
- ViewAnimator
- ViewFlipper
- ViewSwitcher
- ZoomButtonsController
- ZoomButton
- ZoomControls
- dalvik.system
布局资源
布局资源
译者微博: http://weibo.com/popapa
版本:Android 3.2 r1
原文
http://developer.android.com/guide/topics/resources/layout-resource.html
参见
布局资源
布局(layout)资源用于定义 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> ,但只能有一个根元素且必须包含带 android 命名空间的 xmlns:android 属性,如上例所示。
元素:
作为 View 元素的容器, ViewGroup 对象分很多种类,每一类都能用不同的方式指定其子元素的布局。 ViewGroup 对象的种类包括: LinearLayout 、 RelativeLayout 和 FrameLayout 。
并不是所有的 ViewGroup 派生类都能容纳嵌套的 View 。 有些 ViewGroup 是 AdapterView 类的实现,因此就只能容纳 Adapter 类型的子元素。
属性:
android:id
资源 ID 。为元素指定一个唯一的资源名称,用于应用程序能实现对 ViewGroup 的引用。详见下文 android:id 的值 。
android:layout_height
度量或关键字。 必填项 。高度,用度量值、度量资源 ( dimension resource ) 或关键字"fill_parent" 、"wrap_content"指定。参阅下文 合法值 。
android:layout_width
度量或关键字。 必填项 。宽度,用度量值、度量资源 ( dimension resource ) 或关键字"fill_parent" 、"wrap_content"指定。参阅下文 合法值 。
ViewGroup 基类支持更多的属性,每种子类的实现也支持很多其他属性。完整的可用属性请参阅各个类相关的参考文档,例如 LinearLayout XML attributes 。
一个独立的 UI 控件,通常是指一个部件(widget)。 View 对象的种类包括: TextView 、 Button 和 CheckBox 。
属性:
android:id
资源 ID 。为元素指定一个唯一的资源名称,用于应用程序能实现对 ViewGroup 的引用。详见下文 android:id 的值 。
android:layout_height
度量或关键字。 必填项 。高度,用度量值、度量资源 ( dimension resource ) 或关键字"fill_parent" 、"wrap_content"指定。参阅下文 合法值 。
android:layout_width
度量或关键字。 必填项 。宽度,用度量值、度量资源 ( dimension resource ) 或关键字"fill_parent" 、"wrap_content"指定。参阅下文 合法值 。
View 基类支持更多的属性,每种子类的实现也支持很多其他属性。更多信息请参阅 声明 Layout 。完整的可用属性请参阅各个类相关的参考文档,例如 TextView XML 属性 。
将屏幕的初始焦点设成其父元素,任何表示 View 类对象的元素都能包含这个内容为空的元素。但每个文件内只能出现一次本元素。
将另一个布局(layout)文件包含进来。
属性:
layout
Layout 资源 。 必填项 。引用布局资源。
android:id
资源 ID 。覆盖包含进来的 layout 资源中的根 view ID。
android:layout_height
度量或关键字。 覆盖包含进来的 layout 资源中根 view 给出的高度。仅在同时给出 android:layout_width 时才生效。
android:layout_width
度量或关键字。 覆盖包含进来的 layout 资源中根 view 给出的高度。仅在同时给出 android:layout_height 时才生效。
只要是被包含的 layout 资源根元素支持的属性,都能在<include>元素中包含进来,并且会覆盖本资源内根元素已定义的属性。
注意 : 如果要覆盖 layout 的度量(长度和宽度),必须同时覆盖 android:layout_height 和 android:layout_width——不能只覆盖长度或只覆盖宽度。如果只覆盖其中一个,则不会生效。(其他布局属性,比如 weight,仍然继承自原有的 layout。)
包含 layout 资源的另一种方式是使用 ViewStub 。这是个轻量级的 View,它在实际被填充之前不占用 layout 空间。在实际被填充时,它再把 android:layout 属性指定的 layout 资源文件动态包含进来。使用 ViewStub 的更多信息请参阅 Layout 特技: ViewStubs 。
在 layout 的层次结构图里并没画出这个可充当根的元素。当明确知道本 layout 会被放入某个父 View 中去时,可以用<merge>作为根元素来包裹其下的子元素。如果希望本 layout 能被其他 layout 用 <include> 包含进去,并不再另外生成 ViewGroup 容器,本元素也特别有用。合并 layout 的更多信息请参阅 Layout 特技:合并 。
对于 ID 的值,通常应该用"@+id/ name "的语法格式。加号+表示这是个新的资源 ID。如果 R.java 类中不存在此资源的话,aapt 工具将在 R.java 类中生成一个新的 resource 整数标识。例如:
<TextView android:id="@+id/nameTextbox"/> |
名称 nameTextbox 现在作为资源 ID 和本元素关联。然后在 Java 代码里可以用此 ID 名称来引用这个 TextView :
findViewById(R.id.nameTextbox) ; |
本行代码返回 TextView 对象。
然而,如果已经定义了一个 ID 资源 (并且没被使用过),就可以去掉 android:id 里的加号并将此 ID 赋予 View 元素。
android:layout_height 和 android:layout_width 的值
高度和宽度值能用 Android 支持的 度量单位 (px, dp, sp, pt, in, mm)或者以下关键字来表示:
值 | 描述 |
match_parent | 和父元素匹配(充满)。自 API Level 8 开始加入,以淘汰 fill_parent。 |
fill_parent | 和父元素匹配(充满)。 |
wrap_content | 仅本元素内容需要的大小。 |
自定义 View 元素
可以生成自定义的 View 和ViewGroup 元素并且和标准布局元素一样放入 layout。还可以在 XML 元素内定义其所支持的属性。更多内容请参阅 创建自定义控件 。
示例:
存放在 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:layout_height="wrap_content" android:text="Hello, I am a Button" /> </LinearLayout> |
以下代码在 Activity 的 onCreate() 方法中装入 layout。
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView.(R.layout.main_activity); } |
参见
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论