Android中的layout

1. Linear Layout中的weight和gravity的作用。

先看看下面的例子:

<?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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="100dp"
        android:gravity="right"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/to" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:gravity="bottom"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>

效果是:

从上图可以知道,weight的计算方法是:

1+1+2=4,所有第一个EditText会占总空间(除过最后一个EditText)的25%,第二个也是25%,第三个是50%。

gravity的作用是View的内容的显示方式是从哪里开始。

(Specifies how an object should position its content, on both the X and Y
axes, within its own bounds. )

http://developer.android.com/reference/android/widget/LinearLayout.html

2. Relative layout中的参数

XML Attributes

Attribute Name | Related Method | Description
android:layout_above
| | Positions the bottom edge of this view above the given anchor view ID.
android:layout_alignBaseline
| | Positions the baseline of this view on the baseline of the given anchor
view ID.
android:layout_alignBottom
| | Makes the bottom edge of this view match the bottom edge of the given
anchor view ID.
android:layout_alignEnd
| | Makes the end edge of this view match the end edge of the given anchor
view ID.
android:layout_alignLeft
| | Makes the left edge of this view match the left edge of the given anchor
view ID.
android:layout_alignParentBottom
| | If true, makes the bottom edge of this view match the bottom edge of the
parent.
android:layout_alignParentEnd
| | If true, makes the end edge of this view match the end edge of the
parent.
android:layout_alignParentLeft
| | If true, makes the left edge of this view match the left edge of the
parent.
android:layout_alignParentRight
| | If true, makes the right edge of this view match the right edge of the
parent.
android:layout_alignParentStart
| | If true, makes the start edge of this view match the start edge of the
parent.
android:layout_alignParentTop
| | If true, makes the top edge of this view match the top edge of the
parent.
android:layout_alignRight
| | Makes the right edge of this view match the right edge of the given
anchor view ID.
android:layout_alignStart
| | Makes the start edge of this view match the start edge of the given
anchor view ID.
android:layout_alignTop
| | Makes the top edge of this view match the top edge of the given anchor
view ID.
android:layout_alignWithParentIfMissing
| | If set to true, the parent will be used as the anchor when the anchor
cannot be be found for layout_toLeftOf, layout_toRightOf, etc.
android:layout_below
| | Positions the top edge of this view below the given anchor view ID.
android:layout_centerHorizontal
| | If true, centers this child horizontally within its parent.
android:layout_centerInParent
| | If true, centers this child horizontally and vertically within its
parent.
android:layout_centerVertical
| | If true, centers this child vertically within its parent.
android:layout_toEndOf
| | Positions the start edge of this view to the end of the given anchor
view ID.
android:layout_toLeftOf
| | Positions the right edge of this view to the left of the given anchor
view ID.
android:layout_toRightOf
| | Positions the left edge of this view to the right of the given anchor
view ID.
android:layout_toStartOf
| | Positions the end edge of this view to the start of the given anchor
view ID.


http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

http://developer.android.com/guide/topics/ui/declaring-layout.html