Android开发常用布局

        所谓布局就是组件在Activity中的呈现方式,是组件大小、间距和对齐方式等 在Android中常见的布局方式有如下几种:

  • 线性布局(LinearLayout):线性布局,按照垂直或者水平方向布局组件。
  • 帧布局(FrameLayout):组件从屏幕的左上角坐标布局组件。
  • 表格布局(TableLayout):按照行列方式布局组件。
  • 相对布局(RelativeLayout):相对其它组件的布局方式。
  • 绝对布局(AbsoluteLayout):按照绝对坐标来布局。
  • 网格布局(GridLayout):以网格形式进行布局。
  • 约束布局(Constraintlayout):使用约束的方式指定各个控件的位置和关系来进行布局。

一、LinearLayout

        是用的最多的布局,也是最简单的布局。其中的View都是按顺序排列的,而且只能是在一个方向上排列。

  • android:orientation    指定方向(horizental/vertical)
  • android:width            容器宽度(**dp,wrap_content,match_parent)
  • android:height           容器高度(**dp,wrap_content,match_parent)
  • android:background  背景(#000, @android:color/black, @color/myColorStyle)
  • android:gravity          子容器相对于父容器所在的位置

二、RelativeLayout

        每个View都是相对另一个View来确定位置,也就是允许子元素指定它们相对于其父元素或兄弟元素的位置,如果你要删除某个View,则很可能牵连其他的View。

第一类:属性值为true或false

  • android:layout_centerHrizontal                           水平居中
  • android:layout_centerVertical                             垂直居中
  • android:layout_centerInparent                            相对于父元素完全居中
  • android:layout_alignParentBottom                      贴紧父元素的下边缘
  • android:layout_alignParentLeft                           贴紧父元素的左边缘
  • android:layout_alignParentRight                         贴紧父元素的右边缘
  • android:layout_alignParentTop                           贴紧父元素的上边缘
  • android:layout_alignWithParentIfMissing    如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”

  • android:layout_below                          在某元素的下方
  • android:layout_above                          在某元素的的上方
  • android:layout_toLeftOf                       在某元素的左边
  • android:layout_toRightOf                     在某元素的右边
  • android:layout_alignTop                      本元素的上边缘和某元素的的上边缘对齐
  • android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐
  • android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐
  • android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px     

  • android:layout_marginBottom              离某元素底边缘的距离   
  • android:layout_marginLeft                   离某元素左边缘的距离   
  • android:layout_marginRight                 离某元素右边缘的距离   
  • android:layout_marginTop                   离某元素上边缘的距离

三、AbsoluteLayout

        绝对定位,又可以叫做坐标布局,不常用,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。

四、GridLayout

  • GridLayout布局相关属性:rowCount、columnCount      
  • GridLayout中子控件相关属性:layout_gravity="fill_horizontal|fill_vertical"

五、ConstraintLayout

        约束布局,使用约束的方式来指定各个控件的位置和关系,如无任何约束条件默认显示在右上方,有点 类似于RelativeLayout,但远比RelativeLayout要更强大。可以有效地解决布局嵌套过多的问题,以往编写复杂的界面总会伴随着多层的嵌套,设备绘制视 图所需的时间和计算功耗也就越多,从而导致程序卡顿甚至崩溃。

        这些属性的值即可以是parent, 也可以是某个view的id。

  • layout_constraintLeft_toLeftOf 
  • layout_constraintLeft_toRightOf 
  • layout_constraintRight_toLeftOf 
  • layout_constraintRight_toRightOf 
  • layout_constraintTop_toTopOf 
  • layout_constraintTop_toBottomOf 
  • layout_constraintBottom_toTopOf 
  • layout_constraintBottom_toBottomOf 
  • layout_constraintBaseline_toBaselineOf 
  • layout_constraintStart_toEndOf 
  • layout_constraintStart_toStartOf 
  • layout_constraintEnd_toStartOf 
  • layout_constraintEnd_toEndOf

角度定位:角度定位用一个角度和一个距离来确定与目标控件中心的位置 常用属性:

  • app:layout_constraintCircle="@+id/×××" (目标控件id)
  • app:layout_constraintCircleAngle="××"( 角 度 ) 
  • app:layout_constraintCircleRadius="××dp"(距离)

ConstraintLayout的边距常用属性如下:

        控件在ConstraintLayout里面要实现margin, 必须先约束该控件在ConstraintLayout里的 位置,否则默认显示在左上角,margin只能大于等于0。

  • android:layout_marginStart 
  • android:layout_marginEnd 
  • android:layout_marginLeft 
  • android:layout_marginTop 
  • android:layout_marginRight 
  • android:layout_marginBottom

        边距 goneMargin主要用于约束的控件可见性被设置 为gone的时候使用的margin值:

  • layout_goneMarginStart 
  • layout_goneMarginEnd 
  • layout_goneMarginLeft 
  • layout_goneMarginTop 
  • layout_goneMarginRight 
  • layout_goneMarginBottom

      位置偏移 如果想让view的位置偏向某一侧, 可以使用以下的两个属性来设置:

  • layout_constraintHorizontal_bias    //水平偏向
  • layout_constraintVertical_bias    //竖直偏向

设置权重,在对应的方向上,应该为0

  • app:layout_constraintHorizontal_weight //水平权重 
  • app:layout_constraintVertical_weight //竖直权重

在设置宽高比的值的时候,还可以在前面加W或H,分别 指定宽度或高度限制。 例如:

  • app:layout_constraintDimensionRatio="H,2:3"  
  • app:layout_constraintDimensionRatio="W,2:3"

layout_constraintHorizontal_chainStyle来改 变整条链的样式。提供了3种样式,分 别是:

  • CHAIN_SPREAD —— 展开元素 (默认); 
  • CHAIN_SPREAD_INSIDE —— 展开元素, 但链的两端贴近parent; 
  • CHAIN_PACKED —— 链的元素将被打包在 一起。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值