基础篇1.6 Android Layout
1、 LinearLayout(线性布局)
(1) 应用举例:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--
android:id为控件指定相应的id
android:text指定控件当中显示的文字,尽量使用@string
android:grivity指定控件的基本位置
android:textSize指定控件当中字体的大小
android:background指定控件的背景颜色
android:width控件的宽度
android:heigth控件的高度
android:padding*控件的内边框
android:singleLine是否在一行中显示
android:layout_*指定控件的高度和宽度布局
-->
<TextView
android:id="@+id/firstText"
android:text="第一行"
android:gravity="center_vertical"
android:textSize="35pt"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="20dip"
android:paddingRight="30dip"
android:paddingBottom="10dip"
android:layout_weight="1"
android:singleLine="true"/>
<TextView
android:id="@+id/secondText"
android:text="第二行"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#0000aa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
(2) 运行结果
2、 TableLayout(表格布局)
(1) 应用举例
<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
tools:context=".MainActivity">
<TableRow>
<TextView
android:text="row1_column1"
android:padding="3dip"/>
<TextView
android:text="row1_column2"
android:padding="3dip"
android:gravity="center_horizontal"/>
<TextView
android:text="row1_column3"
android:padding="3dip"
android:gravity="right"/>
</TableRow>
<TableRow>
<TextView
android:text="row2_column1"
android:padding="3dip"/>
<TextView
android:text="row2_column2"
android:padding="3dip"
android:gravity="right"/>
</TableRow>
</TableLayout>
(2) 运行结果
3、 RelativeLayout(相对布局)
(1) 应用举例
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
tools:context=".MainActivity">
<!--
android:layout_above 将该控件的底部置于给定ID的控件之上
android:layout_below 将该控件的底部置于给定ID的控件之下
android:layout_toLeftOf 将该控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf 将该控件的右边缘和给定ID控件的左边缘对齐
android:layout_alignBaseline 该控件的baseline和给定控件的baseline对齐
android:layout_alignBottom 该控件的底部边缘与给定ID控件的底部边缘对齐
android:layout_alignLeft 该控件的左边缘与给定ID控件的左边缘对齐
android:layout_alignRight 该控件的右边缘与给定ID控件的右边缘对齐
android:alignParentBottom 如果该值为true,则该控件的底部与父控件的底部对齐
android:layout_alignParentLeft 如果该值为true,则该控件的左边与父控件的左边对齐
android:layout_alignParentRight 如果该值为true,则该控件的右边与父控件的右边对齐
android:layout_alignParentTop 如果该值为true,则该控件的顶部与父控件的顶部对齐
android:layout_centerHorizontal 如果为true,该控件将置于水平方向的中央
android:layout_centerInParent 如果为true,该控件将置于父控件的中央
android:layout_centerVertical 如果为true,该控件将置于垂直方向中央
-->
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Input:"/>
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/myTextView"/>
<Button
android:id="@+id/myButton01"
android:layout_below="@+id/myEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20px"
android:text="OK"/>
<Button
android:id="@+id/myButton02"
android:layout_toLeftOf="@+id/myButton01"
android:layout_alignTop="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</RelativeLayout>
(2) 运行结果