参考资料:http://developer.android.com/guide/topics/ui/declaring-layout.html
A layout definesthe visual structure for a user interface, such as the UI for an activity or appwidget. You can declare a layout in two ways:
布局定义对用户界面的可视化结构,比如activity或 app widget
· Declare UI elements in XML.在XML文件中声明元素
· Instantiate layout elements at runtime.运行时,实例化布局元素
The advantage to declaring your UI in XML isthat it enables you to better separate the presentation of your applicationfrom the code that controls its behavior. Your UI descriptions are external toyour application code, which means that you can modify or adapt it withouthaving to modify your source code and recompile.
在XML声明UI的好处是,它能使你更好地将应用的呈现和行为控制的代码分离。你的UI描述在应用代码的外部,意味着你可以修改或调整它,而不用修改你的源代码并重编译
Each layout file must contain exactly one rootelement, which must be a View or ViewGroup object. Once you've defined the rootelement, you can add additional layout objects or widgets as child elements togradually build a View hierarchy that defines your layout.
每个布局必须只包含一个根元素,其类型为View或ViewGroup.一旦你定义了根元素,你可以添加子元素,以逐渐建立一个视图的层级结构
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tbz.sysu.clock.MainActivity">
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="138dp"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timePicker1"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="Button"/>
</RelativeLayout>
只允许竖屏: android:screenOrientation="portrait"
Loadthe XML Resource
protected void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Attributes
id 唯一标识符,当需要再次引用时,必须设置
android:id="@+id/button1" @表位置,+表添加
Button sent =(Button)findViewById(R.id.button1); //与之对应的方法
Layout Parameters 布局参数
XML layout attributes named layout_
something define layout parameters
形如layout_something的XML属性
layout_width/layout_height:
match_parent 匹配父控件
wrap_content 包含内容
Layout Position 位置
The geometry of a view is that of arectangle. get
基准点为左上角的顶点
Size, Padding and Margins 大小、内边距和外边距
Common Layout 常用布局
Linear Layout:A layoutthat organizes its children into a single horizontal or vertical row. Itcreates a scrollbar if the length of the window exceeds the length of thescreen.
线性布局:
组织其子视图为一个水平或垂直的单行。若窗口长度超过屏幕长度,则创建一个滚动条
Relative Layout:Enables youto specify the location of child objects relative to each other (child A to theleft of child B) or to the parent (aligned to the top of the parent).
相对布局:能够让你确定子对象相对于彼此或父控件的位置
Web View:Displays web pages.
Note: Although you can nest one or more layoutswithin another layout to acheive your UI design, you should strive to keep yourlayout hierarchy as shallow as possible. Your layout draws faster if it hasfewer nested layouts (a wide view hierarchy is better than a deep viewhierarchy).
注意:虽然你可以嵌套布局,但你应该努力去使你的布局层级结构尽可能的小。这样才可提高绘图效率(更广度的层级优于更深度的层级)
BuildingLayouts with an Adapter
When the content for your layout is dynamic or notpre-determined, you can use a layout that subclassesAdapterView to populate 填入 the layout with views at runtime. A subclass of the AdapterView class uses an Adaptertobind data to its layout. The Adapter behaves as a middle-man between the data source and the AdapterViewlayout—the Adapter retrieves the data (from a source such as an array or adatabase query) and converts each entry into a view that can be added into the AdapterView layout.
当你的布局内容是动态的或不能预先确定的,那么你可以使用AdapterView的布局子类来填入视图到布局中。Adapter绑定数据和布局,充当中间人,检索并转换数据,添加到AdapterView视图
List View:Displays ascrolling single column list. 显示可滚动的单列表
Grid View:Displays ascrolling grid of columns and rows.显示一个可滚动的行列格子