Activity(活动)中包含views(视图)和ViewGroups(视图组)。
views继承自android.view.view
views 包括button textview deittext ...
viewgroups继承自android.view.viewgroup
包括
- LinearLayout
- AbsoluteLayout
- TableLayout
- RelativeLayout
- FrameLayout
- <ScrollView>
每一个View和ViewGroup有一些共同的属性,一些例子:
LinearLayout把视图组织成一行或一列
属性 | 描述 |
layout_width | 指定View或ViewGroup的宽度 |
layout_height | 指定View或ViewGroup的高度 |
layout_marginTop | 指定View或ViewGroup上方的额外空间 |
layout_marginBottom | 指定View或ViewGroup下方的额外空间 |
layout_marginLeft | 指定View或ViewGroup左侧的额外空间 |
layout_marginRight | 指定View或ViewGroup右侧的额外空间 |
layout_gravity | 指定View或ViewGroup中的子视图的排列位置 |
layout_weight | 指定指派给View或ViewGroup的额外空间尺寸 |
layout_x | 指定View或ViewGroup的x坐标 |
layout_y | 指定View或ViewGroup的y坐标 |
使用AbsoluteLayout,可以指定它其中的子View的确切位置。观察如下main.xml中的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <Button
- android:layout_width="188dp"
- android:layout_height="wrap_content"
- android:layout_x="126px"
- android:layout_y="361px"
- android:text="Button" />
- <Button
- android:layout_width="113dp"
- android:layout_height="wrap_content"
- android:layout_x="12px"
- android:layout_y="361px"
- android:text="Button" />
- </AbsoluteLayout>
TableLayout可以把视图views组织成“行”或“列”。可以使用<TableRow>元素指定表格中的一行。每一行又可以包含一个或多个视图。每行中的每个视图组成了表格的一个元素。每列的宽度,取决于这一列中宽度最大的视图view。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:text="User Name:"
android:width="120dp" />
<EditText
android:id="@+id/txtUserName"
android:width="200dp" />
</TableRow>
<TableRow>
<TextView android:text="Password:" />
<EditText
android:id="@+id/txtUserName"
android:password="true" />
</TableRow>
<TableRow>
<TextView />
<CheckBox
android:id="@+id/chkRememberPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Remember Password" />
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonSignIn"
android:text="Log In" />
</TableRow>
</TableLayout>
模拟器上的效果图:
以上的例子,TableLayout中有2列,4行。在“Password” TextView视图的正下方,是一个空的<TextView>元素。如果不这么做的话,“Remember Password” CheckBox就会出现在“Password”TextView视图的下面,就像这样:
![](https://img-my.csdn.net/uploads/201204/05/1333632194_5232.jpg)
使用RelativeLayout,可以通过设置“相对位置”(每个View相对于另一个view的位置),来指定它所包含的子view的位置。看下面main.xml中的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/lblComments"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Comments" />
- <EditText
- android:id="@+id/txtComments"
- android:layout_width="fill_parent"
- android:layout_height="170px"
- android:layout_alignLeft="@+id/lblComments"
- android:layout_below="@+id/lblComments"
- android:layout_centerHorizontal="true"
- android:textSize="18sp" />
- <Button
- android:id="@+id/btnSave"
- android:layout_width="125px"
- android:layout_height="wrap_content"
- android:layout_alignRight="@+id/txtComments"
- android:layout_below="@+id/txtComments"
- android:text="Save" />
- <Button
- android:id="@+id/btnCancel"
- android:layout_width="124px"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/txtComments"
- android:layout_below="@+id/txtComments"
- android:text="Cancel" />
- </RelativeLayout>
- layout_alignParentTop
- layout_alignParentLeft
- layout_alignRight
- layout_below
- layout_centerHorizontal
这些属性的值,就是每个被引用的view的id。
效果图: