Android有四种常用的布局LinearLayout(线性布局),RelativeLayout(相对布局),FrameLayout(幡帧布局),TableLayout(表格布局)。
1:LinearLayout又称作线性布局,是一种非常常用的布局。正如他的名字一样,这个布局会将她的控件在线性方向上依次排列;
xml源代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <Button        android:id="@+id/btn11"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_gravity="top"        />    <Button        android:id="@+id/btn12"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        android:layout_gravity="center_vertical"        />    <Button        android:id="@+id/btn13"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button3"        android:layout_gravity="bottom"        />LinearLayout>

效果是这样的:

通过android:orirntation属性制定了排列方向,如果指定了排列方向是vertical,则是在垂直方向上依次排列,指定horizontal方向则是再水平方向上排列。注意android:layout_gravity属性,他是来指定排列方式的;
2:RelativeLayout又称作相对布局,也是一种非常常用的布局。和LinearLayout的排列则不同,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" >    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button3"        android:layout_centerInParent="true"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button4"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button5"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        />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" >    <Button         android:id="@+id/bt31"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_centerInParent="true"        />    <Button         android:id="@+id/btn32"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        android:layout_above="@id/bt31"        android:layout_toLeftOf="@id/bt31"        />    <Button         android:id="@+id/bt33"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button3"        android:layout_above="@id/bt31"        android:layout_toRightOf="@id/bt31"        />    <Button         android:id="@+id/bt34"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button4"        android:layout_below="@id/bt31"        android:layout_toLeftOf="@id/bt31"        />    <Button         android:id="@+id/bt35"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button5"        android:layout_below="@id/bt31"        android:layout_toRightOf="@id/bt31"        />RelativeLayout>


3: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" >    <Button         android:id="@+id/btn41"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button"        />    <ImageView         android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/qqjietu"        />FrameLayout>


4:TableLayout允许我们使用表格的方式来排列控件,这种布局也不是很常用,你只需要了解一下它的基本用法就可以了。既然是表格,那就一定会有行和列,在设计表格时我们尽量应该让每一行都拥有相同的列数,这样的表格也是简单的。不过有时候事情并非总会顺从我们的心意,当表格的某一行一定要要求不相等的列数时,就需要通过合并单元格的方式来应对;

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:stretchColumns="1">    <TableRow>      <TextView           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="账号:"/>      <EditText           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:hint="请输入账号"/>      TableRow>    <TableRow>        <TextView           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="密码:"/>      <EditText           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:hint="请输入密码"/>    TableRow>    <TableRow>        <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_span="2"            android:text="登陆"/>    TableRow>TableLayout>


注意tablelayout里面无法指定控件的宽度,这个时候可以用android:stretchColumns的值就可以;例:android:stretchColumns=”1”,是当控件不能完全充满屏幕宽度的时候能把第二个控件宽度拉伸充满屏幕宽度。

更多相关文章

  1. RelativeLayout各个属性
  2. android 焦点问题
  3. Your content must have a ListView whose id attribute is 'and
  4. Android(安卓)Layout XML属性
  5. Android入门第十六篇之Style与Theme
  6. Android学习-RelativeLayout相对布局属性方法
  7. RelativeLayout常用布局属性
  8. Android学习笔记十五之TableLayout表格布局
  9. android:contentDescription 的用途

随机推荐

  1. 开发Android第一步,安装SDK 1.6, 模拟器及
  2. android 录音机&示波器
  3. Android(安卓)App 增量更新实例(Smart App
  4. Android(安卓)兼容获取Mac地址与手机信息
  5. 修改AndroidStudio默认的compileSdkVersi
  6. 数据库表的查询操作实践演练(实验三)
  7. 解决“Connection to https://dl-ssl.goo
  8. 通過android selector改变界面状态
  9. 数据库表的查询操作(实验二)
  10. 数据库表的创建、管理和数据操作(实验一)