Android UI的开发博大精深,在此总结一下我所学习的布局的知识。
在Android应用程序中,界面通过布局文件设定。布局文件采用XML格式,语法是类似于HTML的脚本语言。每个XML文件需要用onCreate()方法加载到对象中进行显示。具体代码如下:
public class MainActivity extends Activity{
protect void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
此外,Android的布局分为6种,分别是相对布局、线性布局、表格布局、网格布局、帧布局和绝对布局。
一、相对布局
相对布局是一种常用的布局方法,通常是相对于容器而言或者是相对于控件而言,用RelativeLayout标签定义。为了准确定位控件的,相对布局定义了一系列属性。
android:layout_marginTop="25dip" //顶部距离
android:gravity="left" //空间布局位置
android:layout_marginLeft="15dip //距离左边距
android:layout_above 将该控件的底部置于给定ID的控件之上;
android:layout_below 将该控件的底部置于给定ID的控件之下;
android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐;
android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐;
android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;
android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐;
android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐;
android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;
android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐;
android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐;
android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;
android:layout_centerVertical 如果为true,将该控件的置于垂直居中;
android:layout_centerInParent 如果为true,将该控件的置于父控件的中央;
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft 左偏移的值;
android:layout_marginRight 右偏移的值;
二、线性布局
线性布局分为两种,一种是水平线性布局,一种是垂直线性布局。它使用LinearLayout标签表示。
线性布局中有一个标签orientantion,该属性的取值有vertical(垂直)和horizontal(水平)。
三、表格布局
表格布局的控件排列要比线性布局整齐很多,用标签TableLayout。
在表格布局中,用TableRow控制行数,即布局中有多少行就有多少TableRow,每一行中可以放置多个控件,控件中通过android:layout_column属性指定具体的列数,该属性的值是从0开始的。
需要注意的是,TableRow不需要设置宽度(默认为match_parent)和高度(默认为warp_content)。
四、网格布局
这种布局方式类似于表格布局,但是它能够更好的实现控件的交错显示。网格布局用一组无限细的直线将绘图区域分成行、列和单元,指定空间的显示区域和现实方式。网格布局用GridLayout标签表示。
五、帧布局
帧布局为每个控件创建一片空白区域(称为一帧),在左上角显示,添加多个控件时,会透明显示之前的控件的文本。帧布局用标签FrameLayout表示。
六、绝对布局
绝对布局需要用x,y坐标来控制控件的位置,用AbsoluteLayout标签来表示。其中坐标的控制语句为android:layout_x和android:layout_y。
在上文中指定控件位置的单位一般有4种:
1、px:代表像素
2、pt:代表磅数
3、dp:代表密度无关单位,又称dip,推荐控件与布局时使用
4、sp:代表可伸缩像素,采用与dp相同的设计理念,推荐设置文字大小时使用
另附一个用户注册页面的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.donggaojun.myapplication.MainActivity">
<LinearLayout
android:id="@+id/regist_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="10dp"
android:text="用户名:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的用户名"
android:textSize="14dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/regist_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/register_username"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="10dp"
android:text="密 码:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码"
android:inputType="textPassword"
android:textSize="14dp"/>
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_password"
android:layout_marginLeft="80dp"
android:contentDescription="性别"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男">
</RadioButton>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女">
</RadioButton>
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="注册"/>
</RelativeLayout>