安卓基础篇《Android 五大布局基本使用》

在开始讲解5大布局前,我们先看下一种被Android studio默认的新型布局:

ConstraintLayout 约束布局:

            

        概念:ConstraintLayout约束布局的含义: 根据布局中的其他元素或视图, 确定View在屏幕中的位置, 受到三类约束, 即其他视图, 父容器(parent), 基准线(Guideline).

        在2016年由Google I/O推出. 从支持力度而言, 将成为主流布局样式, 完全代替其他布局, 减少布局的层级, 优化渲染性能. 在新版Android Studio中, ConstraintLayout已替代RelativeLayout, 成为HelloWorld项目的默认布局.

展示点:

    1.相对定位:

        layout_constraint[本源位置]_[目标位置]="[目标ID]"

        app:layout_constraintBottom_toBottomOf="parent  |@+id/xxxxid"

layout_constraintLeft_toLeftOf =" praent/id"//将自己部位的左边位于父容器/目标的左边 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

    2.边距设置: android:layout_marginLeft=“10dp”//左边距10dp android:layout_marginTop android:layout_marginRight android:layout_marginBottom

android:layout_marginStart android:layout_marginEnd

3.当第一个参照物隐藏时,第二个view边距不变,要用到以下属性:

    layout_goneMarginStart     layout_goneMarginEnd     layout_goneMarginLeft     layout_goneMarginTop     layout_goneMarginRight     layout_goneMarginBottom

     1.RelativeLayout 相对布局:

  • 布局特点:为某一个组件为参照物,来定位下一个组件的位置的布局方式。
  • 应用场景:控件之间存在相应关系(适配神器,推荐使用)

    

    展示点: 常用的一些属性

    第一类:属性值为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 离某元素上边缘的距离

    隐藏其中一子控件,观察布局变化

    

2. LinearLayout线性布局

  • 布局特点:放主要提供控件水平或者垂直排列的模型,每个子组件 

    都是以垂直或水平的方式来线性排布.(默认是垂直)

  • 应用场景:最常用的布局方式
  • linearLayout中有一个重要的属性 android:layout_weight=”1”,这个weight在垂直布局时,代表行距;水平的时候代表列宽;weight值越大就越大。

    

    展示点:

            1.android:orientation="vertical/horizontal"

            2.android:layout_height="0dp"       android:layout_weight="1"

            3.隐藏其中一子控件,观察布局变化

            

3. FrameLayout框架布局

  • 布局特点:放入其中的所有元素都被放置在最左上的区域,而且无法为这些元素指定一个确切的位置,下一个子元素会重叠覆盖上一个子元素
  • 应用场景:适合浏览单张图片。

    展示点:三张有色图,大中小互相叠加

4.AbsoluteLayout绝对定位布局

  • 布局特点:采用坐标轴的方式定位组件,左上角是(0,0)点,往右x轴递增,往下Y轴递增,组件定位属性为android:layout_x和 android:layout_y来确定坐标。
  • 应用场景:准确定位空间位置、

    展示点:1.      -xxdp

            2.      大于父布局高度

      5.TableLayout表格布局

  • 布局特点:类似Html里的Table.使用TableRow来布局,其中TableRow代表一行,TableRow的每一个视图组件代表一个单元格。
  • 应用场景:控件之间存在相应关系。

    展示点: <TableRow/> 行          <TableLayout/> 列 

                android:divider="@drawable/line_h"

                android:stretchColumns="*"//拉伸子控件会平分本行

                android:showDividers="beginning|middle|end"

                三个常用属性

  • android:collapseColumns:设置需要被隐藏的列的序号
  • android:shrinkColumns:设置允许被收缩的列的列序号
  • android:stretchColumns:设置运行被拉伸的列的列序号

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值