android 常用UI 之2--几种主要布局要点

android 布局种类

  1. LinearLayout线性布局
  2. RelativeLayout相对布局
  3. FrameLayout帧布局(我也把它叫做覆盖布局)
  4. AbsoluteLayout绝对布局(已过时,建议不要采用)
  5. TableLayout表格布局(极少使用)
LinearLayout线性布局
LinearLayout线性布局,它包含的子控件将以横向或纵向的方式排列


自己重要属性:
android :orientation= "vertical" ( 纵向
android :orientation= "horizontal" ( 横向

android :gravity= "center" (决定他子控件的x、y坐标的位置。或left 或right等)




  • center_vertical :垂直(Y轴)居中
  • center_horizontal:水平(x轴)居中
  • center:同时垂直水平居中
  • right:子控件位于当前布局的右边
  • left:子控件位于当前布局的左边
  • top:子控件位于当前控件的上面
  • bottom:子控件位于当前控件的下面
  • 这些grvity的值可以组合使用如:android:gravity="right|top"


子类控件重要属性:
android :layout_gravity= "right"( 子控件本身在LinearLayout的X、y轴位置
android :layout_weight= "1" (子控件占父容器的比例)


RelativeLayout相对布局

RelativeLayout相对布局,它包含的子控件将一控件之间的相对位置或子空间相对父容器的位置的方式排列



子类控件在RelativeLayout中常用到的属性(相对应容器)



android :layout_alignParentBottom= "true"子控件相对于父容器靠下面
android :layout_alignParentLeft= "true"子控件相对于父容器靠左边
android :layout_marginRight= "20dp"子控件距离父容器右边的距离(还有左边距离、下面距离等)
android :layout_centerInParent= "true"子控件相对于父容器将同时横向居中和垂直居中(还有横向居中、垂直居中)

RelativeLayout中子类控件相对于其他子类控件的一个位置



  • android:layout_above="@id/btn1" 该控件位于指定控件(@id/btn1)的上面
  • android:layout_below="@id/btn1"该控件位于指定控件(@id/btn1)的下面
  • android:layout_toRightOf="@id/btn1"该控件位于指定控件(@id/btn1)的右边
  • android:layout_toLeftOf="@id/btn1"该控件位于指定控件(@id/btn1)的左边
  • android:layout_alignBottom="@id/btn1"该控件的底部边缘与给定控件的底部边缘对齐(还有其他上面对齐、左边对齐、右边对齐等)

应用场景



FrameLayout帧布局(我也把它叫做覆盖布局)
这种布局中所有的子控件都不能制定放置的位置,它们都是放在这个布局的左上角,并且后面的添加的子控件覆盖在前面的子控件上面,并将前面的控件部分或全部覆盖。
应用场景效果:



< FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android :id= "@+id/activity_main2"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
tools :context= "czg.com.myapplication.Main2Activity" >


< TextView
android :background= "#dad0c5"
android :text= "第一个放置的控件"
android :layout_width= "300dp"
android :layout_height= "300dp"
android :layout_gravity= "center"

android :id= "@+id/tv1" />

< TextView
android :background= "#cc8888"
android :text= "第2个放置的控件"
android :layout_width= "250dp"
android :layout_height= "250dp"
android :layout_gravity= "center"

android :id= "@+id/tv2" />

< TextView
android :background= "#99223e99"
android :text= "第3个放置的控件"
android :layout_width= "200dp"
android :layout_height= "200dp"
android :layout_gravity= "center"

android :id= "@+id/tv3" />
</ FrameLayout >


AbsoluteLayout绝对布局(已过时,建议不要采用)
绝对布局(坐标布局),用于直接指定子控件的x、y轴的绝对坐标,对适配android多机型多种分辨率来说适应性太差,一般不建议使用。
特殊属性:
android :layout_x= "69dp"
android :layout_y= "83dp"
如:
< Button
android :text= "Button"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_x= "69dp"
android :layout_y= "83dp"
android :id= "@+id/button3" />


TableLayout表格布局(极少使用,一般与TableRow配合使
特殊属性:
android :collapseColumns= "1,2"
折叠(隐藏)索引列,隐藏列必须用逗号隔开:如1,2

android :shrinkColumns= "1,2"
收缩索引列(类似execl中的自动换行),当可收缩的列内容太多,采用自动换行不会被挤出屏幕。列之间必须用逗号隔开:如2,4,你可以通过"*"号代替收缩所有列。
android :stretchColumns= "2"
用指定的列,以填满剩余的空白空间(主要用于放置的这行空间控件太少,没法铺满整行时,造成的不美观,类似与前面介绍过的比重属性 weight ),列之间必须用逗号隔开:如2,4,你可以通过"*"号代替收缩所有列。

表格布局内部控件的特殊属性
android :layout_column= "1" 该控件显示在第2列
android :layout_span= "2" 该控件占据2列


推荐一篇:

android 控件监听事件之1——实现的几种写法

http://toutiao.com/item/6404351888426271233/#6649976-qzone-1-57861-3c8d8e8bb11b3fb9a4fd89478693d6c1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值