Android学习笔记(二):Android中的五大布局

来源:互联网 发布:淘宝定金是什么意思 编辑:程序博客网 时间:2024/06/05 20:26

一:线性布局

<?xml version="1.0" encoding="utf-8"?><!-- LinearLayout:表示线性布局;控件按照从左到右,或从上到下依次显示; --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <!-- 线性布局分水平和垂直布局:vertical表示垂直布局;horizontal表示水平布局;    垂直布局的时候,控件会自动按照从上到下的顺序依次排列; -->        <!--     布局中所有的控件第一个字母都是大写     android:id="@+id/tv_reminder":表示给控件设置一个id编号,方便对该控件的调用;    如果写成"@id/",没有"+"号,表示从R.java中调用某个指定的控件;    android:layout_width:设置控件的宽度;    wrap_content:表示控件的宽度根据控件中的内容会自动调整;    match_parent:填充父窗体,即宽度和父窗体宽度一致;    fill_parent:填充父窗体,和match_parent一样;    layout_marginLeft:设置控件的左外边距,即控件和左边控件的距离;    textSize:设置控件上显示的文本的字体的大小;    text:设置控件上显示的文本信息,一般要在values目录下的strings.xml文件中进行设置;        单位:    dp:用于设置控件的大小、位置等;可以根据屏幕的大小,自动调整控件的位置以及大小;    sp:用于设置控件上显示文本的字体大小,只有这一种情况使用sp,其他都用dp;    -->    <TextView        android:id="@+id/tv_reminder"    android:layout_width="fill_parent"    android:layout_height="wrap_content"        android:layout_marginLeft="16dp"     android:layout_marginTop="16dp"    android:textSize="18sp"    android:text="请输入电话号码:"        />        <!--     padding:设置控件的内边距,即控件上显示的文本内容与控件边框的距离;    padding表示对上、下、左、右四个方向的距离都进行设置;    hint:设置控件上的暗示信息,即控件上没有显示内容时,出现的提示信息;    -->    <EditText         android:id="@+id/et_phonenum"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="16dp"        android:layout_marginBottom="16dp"        android:hint="请输入电话号码"        />        <Button         android:id="@+id/btn_dial"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="拨号"        /></LinearLayout>


二:相对布局

<!-- RelativeLayout:相对布局 --><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/tv_input"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="18sp"        android:text="请输入电话号码:" />        <!-- layout_below:设置当前控件在指定控件的下面 -->    <EditText         android:id="@+id/et_num"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/tv_input"        android:hint="请输入手机号码"        />        <!-- layout_centerHorizontal:true表示水平居中; -->    <Button         android:id="@+id/btn_bohao"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/et_num"        android:layout_centerHorizontal="true"        android:text="拨号"/>        <!-- layout_alignLeft:设置当前控件和指定的控件左对齐 -->    <Button         android:id="@+id/btn_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/btn_bohao"        android:layout_alignLeft="@id/btn_bohao"        android:text="按钮"        /></RelativeLayout>

三:帧布局

<?xml version="1.0" encoding="utf-8"?><!-- FrameLayout:表示帧布局,一层一层的显示,嵌套显示; --><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >        <!-- 帧布局用于需要分层显示的情况下,例如视频播放器中间的播放按钮; -->        <!-- textview控件设置宽高都是填充父窗体,全屏显示,如果是线性布局,其他的控件就不会再显示;    但是帧布局可以让其他的控件在该控件的上面继续显示; -->    <TextView         android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="外边框"        />        <!-- Button控件在TextView控件的上方显示     layout_gravity:设置控件的显示布局;    right:表示控件在右方显示;    center_vertical:控件垂直居中;    center:垂直水平都居中;    -->    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="播放"/></FrameLayout>

四:表格布局

<?xml version="1.0" encoding="utf-8"?><!-- TableLayout:表格布局;很少使用; --><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >        <!-- 画一个两行三列的表格 -->        <!-- TableRow:代表一行,里面有几个控件就表示有几列 -->    <TableRow         android:layout_width="match_parent"        android:layout_height="wrap_content">                <!-- TableRow中有三个控件,表示有三列 -->        <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#05CD00"            android:layout_margin="22dp"            android:text="哈哈"/>                <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#B7D9ED"            android:layout_margin="22dp"            android:text="呵呵"/>                        <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#FFAA00"            android:layout_margin="22dp"            android:text="按钮"/>            </TableRow>    <!-- TableRow:两个TableRow表示有两行 -->    <TableRow         android:layout_width="match_parent"        android:layout_height="wrap_content">                <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#05CD00"            android:layout_margin="22dp"            android:text="哈哈"/>                <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#B7D9ED"            android:layout_margin="22dp"            android:text="呵呵"/>                        <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#FFAA00"            android:layout_margin="22dp"            android:text="按钮"/>            </TableRow>    </TableLayout>

五:绝对布局

<?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" >    <!--     layout_x:控件X轴的坐标    layout_y:控件Y轴的坐标    由于绝对布局中控件的位置被写死了,不方便做屏幕的适配,所以绝对布局已经被舍弃了,不再使用;    -->    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="50dp"        android:layout_y="72dp"        android:text="Button" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="186dp"        android:layout_y="64dp"        android:text="Button" /></AbsoluteLayout>