Android的布局介绍

来源:互联网 发布:人工智能会毁灭人类吗 编辑:程序博客网 时间:2024/06/07 03:42

1.线性布局(从上到下,垂直的布局)   android:orientation="vertical"   根元素LinearLayout



2.水平布局(从左到右)   默认  android:orientation="horizontal"    根元素LinearLayout



3.嵌套的线性布局    根元素LinearLayout

<?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"    ><!-- 外部是垂直布局 -->    <!-- 里边是水平布局,注意水平布局的高度是包裹内容 -->    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    >    <Button        android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:text="Button" />    <Button        android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:text="Button" />    <Button        android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:text="Button" /><Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button" />            </LinearLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>



4.帧式布局(将图片跌在某张图片上,先设置的图片在下层,后设置的在上层)   根元素FrameLayout

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" ><!-- "fill_parent"和 match_parent是一样的,都是填充父元素-->    <ImageView     android:src="@drawable/transformers"  android:layout_width="wrap_content"    android:layout_height="wrap_content"     /><ImageView     android:layout_gravity="center"    android:src="@drawable/play"  android:layout_width="50dp"    android:layout_height="50dp"     /><!-- px单位不管屏幕大小都是同一个尺寸dp单位会随着屏幕大小改变而改变图形大小(推荐) --></FrameLayout>



5.表格布局  根元素TableLayout

<?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"    android:stretchColumns="1"><!-- android:stretchColumns="1"将索引为1的列拉伸,指定了宽度后,这个设置就不起作用了,按照宽度来 -->    <TableRow>        <TextView            android:layout_width="0dp"            android:layout_weight="1"            android:padding="0dip"            android:text="1, 1" /><!-- 将文本内容右对齐android:gravity="right",默认是左对齐 android:layout_weight="1"表示权重的意思,数值越大宽度就越大-->        <TextView            android:layout_width="0dp"            android:layout_weight="1"            android:padding="10dip"            android:text="1, 2" />        <TextView            android:layout_width="0dp"            android:layout_weight="1"            android:padding="0dip"            android:text="1, 3" />    </TableRow>    <TableRow>        <TextView            android:padding="10dip"            android:text="2, 1" />        <TextView            android:padding="10dip"            android:text="2, 2" />        <TextView            android:padding="10dip"            android:text="2, 3" />    </TableRow></TableLayout>



6.相对布局  根元素RelativeLayout

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="10dp" ><!-- android:padding表示和四周的边距 -->         <!--    android:layout_marginLeft   距离左边距离   android:layout_below        在哪个组件下面   android:layout_alignTop     和哪个组件的上边距对齐   android:layout_toRightOf在哪个组件的右边   android:layout_alignParentRight对齐父元素的右边    -->    <TextView        android:id="@+id/numTV"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="手机号码"        android:textSize="20sp" />    <EditText        android:id="@+id/numET"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="30dp"        android:layout_toRightOf="@id/numTV" />    <TextView        android:id="@+id/contentTV"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/numET"        android:text="短信内容"        android:textSize="20sp" />    <EditText        android:id="@+id/contentET"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignTop="@id/contentTV"        android:layout_marginLeft="30dp"        android:layout_toRightOf="@id/contentTV"        android:lines="3" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/contentET"        android:text="发送短信" /></RelativeLayout>



7.绝对布局  根元素AbsoluteLayout   靠的是坐标定位

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="16dp"        android:layout_y="9dp"        android:text="Button" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="161dp"        android:layout_y="184dp"        android:text="Button" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="220dp"        android:layout_y="84dp"        android:text="Button" /></AbsoluteLayout>



0 0