Android线性布局,相对布局

Android开发有五种常见布局:
LinearLayout(线性布局),RelativeLayout(相对布局),FrameLayout(帧布局),TableLayout(表格布局),AbsoluteLayout(绝对布局)


LinearLayout线性布局


主要是以水平或垂直的方式来显示页面中的控件,在水平排列情况下,从左到右;垂直情况下,从上到下排列。
1)LinearLayout的属性:orientation (控制控件的排列方向)

  • 1 ,vertical 垂直排列
  • 2,horizontal 水平排列
  • 当控件水平排列时,控件属性layout_width只能设置为wrap_content(包裹内容)。
  • 如果控件属性layout_width设置为match_parent(匹配父窗口),此时只会显示一个控件,其余控件会隐藏不显示。

2)当LinearLayout的layout_widt属性设置为matvh_parent。(也就是当父控件的宽设置为match_parent);
LinearLayout中的所有button按钮的layout_weight属性可以设置为1,1,2,。。
就相当于父控件的宽度为1 (是一个整体),其中的button控件是他的儿子,可以分父控件的宽度。
就是1/(1+1+2)
当所有控件权重都为1时,将平均分配(水平排列时),当前layout_weight/所有layout_weight


layout_weight():

横向排列时horizontal

  • 当两个按钮的宽度都为wrap_content,权重大的,分到的空间大,权重小的,分到的空间小,
  • 当两个按钮的宽度都为match_parent,权重大的,分到的空间小,权重小的,分到的空间大

剩余空间=父控件宽度/高度-所有子控件的宽度/高度之和

水平排列分权重时,把宽设置为0dp,再设置layout_weight
垂直排列分权重时,把高设置为0dp,再设置layout_weight

在这里插入图片描述
在这里插入图片描述


RelativeLayout相对布局(默认)


设置控件位置的属性

  • android:layout_centerInParent() 当前控件为父布局的中间位置,属性值:true,false
  • android:layout_centerVertical() 当前控件在父布局的垂直居中位置,属性值:true,false
  • android:larout_centerHorizontal() 当前控件位于父布局的水平居中位置,属性值:true,false
  • 相对于某控件,在某控件的什么方向
  • android:layout_above(“某控件的id值”) 当前控件位于某控件的上方
  • android:layout_below(“某控件的id值”) 当前控件位于某控件的下方
  • android:layout_toLeftOf(“某控件的id值”) 当前控件位于某控件的左侧
  • android:layout_toRightOf(“某控件的id值”) 当前控件位于某控件的右侧
  • 相对于某控件对齐方式
  • android:layout_alignTop(“某控件的id值”) 当前控件的上边界和某控件的上边界对齐
  • android:layout_alignBottom(“某控件的id值”) 当前控件的下边界和某控件的下边界对齐
  • android:layout_alignLeft(“某控件的id值”) 当前控件的左边界和某控件的左边界对齐
  • android:layout_alignRight(“某控件的id值”) 当前控件的右边界和某控件的右边界对齐
  • 相对于父控件对齐方式
  • android:layout_alignParentTop() 当前控件的上边界是否与父控件的上边界对齐 属性值:true,false
  • android:layout_alignParentBottom() 当前控件的下边界是否与父控件的下边界对齐 属性值:true,false
  • android:layout_alignParentLeft() 当前控件的左边界是否与父控件的左边界对齐 属性值:true,false
  • android:layout_alignParentRight() 当前控件的右边界是否与父控件的右边界对齐 属性值:true,false
发布了57 篇原创文章 · 获赞 1 · 访问量 977

猜你喜欢

转载自blog.csdn.net/qq_45844648/article/details/104906556