1、线性布局 LinearLayout:
       线性布局是所有布局中最常用的类之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。
举个例子:

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. >
  7. <!--
  8. android:id —— 为控件指定相应的ID
  9. android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
  10. android:grivity —— 指定控件的基本位置,比如说居中,居右等位置
  11. android:textSize —— 指定控件当中字体的大小
  12. android:background —— 指定该控件所使用的背景色,RGB命名法
  13. android:width —— 指定控件的宽度
  14. android:height —— 指定控件的高度
  15. android:padding* —— 指定控件的内边距,也就是说控件当中的内容
  16. android:layout_weight —— 控件之间的权重比
  17. android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示
  18. -->
  19. <TextView
  20. android:id="@+id/firstText"
  21. android:text="第一行一行一行一行一行一行一行一行一行一行"
  22. android:gravity="center_vertical"
  23. android:textSize="35pt"
  24. android:background="#aa0000"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:paddingLeft="10dip"
  28. android:paddingTop="20dip"
  29. android:paddingRight="30dip"
  30. android:paddingBottom="40dip"
  31. android:layout_weight="1"
  32. android:singleLine="true"/>
  33. <TextView
  34. android:id="@+id/secondText"
  35. android:text="第二行"
  36. android:gravity="center_vertical"
  37. android:textSize="15pt"
  38. android:background="#0000aa"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_weight="1"/>
  42. </LinearLayout>

复制代码

2、相对布局 RelativeLayout
       相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

举个例子:

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. android:layout_above 将该控件的底部至于给定ID的控件之上
  4. android:layout_below 将该控件的顶部至于给定ID的控件之下
  5. android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
  6. android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐
  7. android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
  8. android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
  9. android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
  10. android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
  11. android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐
  12. android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
  13. android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
  14. android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
  15. android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐
  16. android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
  17. android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
  18. android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央
  19. -->
  20. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent">
  23. <TextView
  24. android:id="@+id/label"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text="Type here:" />
  28. <EditText
  29. android:id="@+id/entry"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:background="@android:drawable/editbox_background"
  33. android:layout_below="@id/label" />
  34. <Button android:id="@+id/ok"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@id/entry"
  38. android:layout_alignParentRight="true"
  39. android:layout_marginLeft="10dip"
  40. android:text="OK" />
  41. <Button android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_toLeftOf="@id/ok"
  44. android:layout_alignTop="@id/ok"
  45. android:text="Cancel" />
  46. </RelativeLayout>

复制代码

3、表单布局 TableLayout
       和TableRow配合使用,和HTML里的Table相似。

举个例子:

java代码:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"android:layout_height="fill_parent" android:stretchColumns="1">
  3. <TableRow>
  4. <TextView
  5. android:layout_column="1"
  6. android:text="打开..."
  7. android:padding="3dip" />
  8. <TextView
  9. android:text="Ctrl-O"
  10. android:gravity="right"
  11. android:padding="3dip" />
  12. </TableRow>
  13. <TableRow>
  14. <TextView
  15. android:layout_column="1"
  16. android:text="保存..."
  17. android:padding="3dip" />
  18. <TextView
  19. android:text="Ctrl-S"
  20. android:gravity="right"
  21. android:padding="3dip" />
  22. </TableRow>
  23. <TableRow>
  24. <TextView
  25. android:layout_column="1"
  26. android:text="另存为..."
  27. android:padding="3dip" />
  28. <TextView
  29. android:text="Ctrl-Shift-S"
  30. android:gravity="right"
  31. android:padding="3dip" />
  32. </TableRow>
  33. <View
  34. android:layout_height="2dip"
  35. android:background="#FF909090" />
  36. <TableRow>
  37. <TextView android:text="*" android:padding="3dip" />
  38. <TextView android:text="导入..." android:padding="3dip" />
  39. </TableRow>
  40. <TableRow>
  41. <TextView android:text="*" android:padding="3dip" />
  42. <TextView android:text="导出..." android:padding="3dip" />
  43. <TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" />
  44. </TableRow>
  45. <View android:layout_height="2dip" android:background="#FF909090" />
  46. <TableRow>
  47. <TextView android:layout_column="1" android:text="退出" android:padding="3dip" />
  48. </TableRow>
  49. </TableLayout>

复制代码

4、切换卡 Tabwidget
       继承TabActivity,实现标签的切换功能。
举个例子:

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. <TabWidget
  11. android:id="@android:id/tabs"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" />
  14. <FrameLayout
  15. android:id="@android:id/tabcontent"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent">
  18. <TextView
  19. android:id="@+id/textview1"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:text="this is a tab" />
  23. <TextView
  24. android:id="@+id/textview2"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:text="this is another tab" />
  28. <TextView
  29. android:id="@+id/textview3"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:text="this is a third tab" />
  33. </FrameLayout>
  34. </LinearLayout>
  35. </TabHost>

复制代码

其他布局:

1、帧布局 FrameLayout:

是最简单的一个布局对象。在他里面的的所有显示对象爱你过都将固定在屏幕的左上角,不能指定位置,但允许有多个显示对象,只是后一个会直接覆盖在前一个之上显示,会把前面的组件部分或全部挡住。

举个例子:

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:text="big"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="50pt"/>
  11. <TextView
  12. android:text="middle"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:textSize="20pt"/>
  16. <TextView
  17. android:text="small"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textSize="10pt"/>
  21. </FrameLayout>

复制代码

2、绝对布局 AbsoluteLayout

绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。分辨率不一样的屏幕,显示的位置也会有所不同。

举个例子:

java代码:

  1. < ?xml version="1.0" encoding="utf-8"?>
  2. < AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. < EditText
  8. android:text="Welcome to Mr Wei's blog"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"/>
  11. < Button
  12. android:layout_x="250px" //设置按钮的X坐标
  13. android:layout_y="40px" //设置按钮的Y坐标
  14. android:layout_width="70px" //设置按钮的宽度
  15. android:layout_height="wrap_content"
  16. android:text="Button"/>
  17. < /AbsoluteLayout>

转载于:https://www.cnblogs.com/xiao0/archive/2011/09/07/2170241.html

转载 Android入门学习_代码常用布局相关推荐

  1. Android 入门第二讲03-约束布局ConstraintLayout(可视化介绍,Chains链,MATCH_CONSTRAIN,百分比布局,圆形定位,Guideline,Barrier)

    Android 入门第二讲03-约束布局ConstraintLayout(可视化介绍,Chains链,MATCH_CONSTRAIN,百分比布局,圆形定位,Guideline,Barrier) 1.可 ...

  2. 深度学习保姆级入门教程 -- 论文+代码+常用工具

    导读 该篇文章可以看作是我研一如何入门深度学习的一个大总结,本人本科专业为软件工程,硕士期间研究方向为基于深度学习的图像分割,跨度相对而言不算太大.如果你对如何入门深度学习还很迷茫的话,那么请看下去吧 ...

  3. Android 学习总结--六大常用布局

    目录 一.相对布局(RelativeLayout) 二.线性布局(LinearLayout) 三.网格布局(GridLayout) 四.表格布局(TableLayout) 五.帧布局(FrameLay ...

  4. Android入门(八) | 常用的界面布局 及 自定义控件

    文章目录 LinearLayout :线性布局 android:layout_gravity :控件的对齐方式 android:layout_weight:权重 RelativeLayout :相对布 ...

  5. Android入门教程:ConstraintLayout约束布局

    原文首发自掘金芦苇APP团队,转载到自己小号上再发一遍~ 翻译By Leelion6.关于 ConstraintLayout 的文章其实已经不少了,不过看到这篇文章写的很有趣,以及在翻译的过程中,感受 ...

  6. android应用开发---(第1章)android基础学习之六大Layout布局

    Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:第一种也是我们最常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根 ...

  7. Android开发学习之卡片式布局的简单实现

    GoogleNow是Android4.1全新推出的一款应用,它可以全面了解你的使用习惯,并为你提供现在或者未来可能用到的各种信息,GoogleNow提供的信息关联度较高,几乎是瞬间返回答案,总而言之, ...

  8. Android入门(二)——常见布局与控件

    文章目录 一.常见界面布局 1.线性布局 LinearLayout 2.相对布局 RelativeLayout 3.表格布局 TableLayout 4.帧布局 FrameLayout 二.常见界面控 ...

  9. 30岁后学oracle还有前途吗,程序员入门学习_程序员30岁后的出路

    下面w3cschool给程序员小伙伴们有些程序员学习编程一上来就想着月薪1W+以上,这样的学习过程会非常痛苦,很 0.C++入门学习方法 首先要对C++有学习基础语法,熟悉调用各种库函数,这时你便成为 ...

最新文章

  1. linux下运行python unitest_Python unittest打印日志可以在Linux上运行,但在Windows上不行...
  2. 这家研究院太年轻,竟跟世界级选手“叫板”
  3. 一个关于解决序列化问题的编程技巧
  4. Django 无法加载静态文件(js,css,image)解决办法
  5. 一个简单的mysql存储过程
  6. request.getParameter和request.getAttribute之间的区别
  7. 2020 MCM Meritorious Winner
  8. 键盘按下某键 停止运行java_实现按下一个键执行操作/松开一个键停止操作
  9. mysql密码命名规则_MySql命名规范
  10. MySQL 数据库误删除后的数据恢复操作说明
  11. sre8 sre10_是什么使SRE出色?
  12. Java Web之会话管理二:Session
  13. 猫叫了,老鼠跑了!(复习委托和事件)
  14. 精通Hyperledger之Hyperledger composer查询语言(17)
  15. 刘宇凡:从吃饭中的道理领悟SEO
  16. Java加密体系结构(JCA)参考指南
  17. ImageNet数据集 下载
  18. cmake + googletest 之一 入门
  19. java锁的种类及研究
  20. Paddle打比赛-古籍文档图像识别与分析算法比赛

热门文章

  1. Maven项目缺少Maven Dependencies解决方法
  2. php 联接sq sever,步骤 4:使用 PHP 弹性连接到 SQL
  3. java中redis存储map集合_使用RedisTemplate存储Map集合的一点注意
  4. java任务追踪预警怎么写_分布式系统中如何优雅地追踪日志(原理篇)
  5. 95-20-025-启动器-AbstractBootstrap
  6. 【clickhouse】Clickhouse 支持毫秒 纳秒数据
  7. 【Antlr】cannot create implicit token for string literal in non-combined grammar xx
  8. Git仓库只拷贝代码-不拷贝提交记录-不拷贝其他分支
  9. RocketMQ写入数据报错RemotingTooMuchRequestException: sendDefaultImpl call timeout
  10. 汇编语言转为c语言,如何把汇编语言转换成C语言