Android Day23

内容

ⅠLinearLayout 线性布局

ⅡRelativelyLayout 相对布局

ⅢConstrainLayout 约束布局

具体内容

ⅠLinearLayout 线性布局

1.Margin与Padding的区别
Margin指的是控件与页面的边缘的间距
而Padding指的是控件内部与空间边缘的间距

2.边距的具体使用
layout_marginStart
layout_marginLeft 左边距

layout_marginRight
layout_marginEnd右边距

layout_marginTop上边距
layout_marginBottom下边距

使用layout_weight权重按比例分配
3.如何更改线性布局的方向
设置Orientation
Vertical表示纵向布局
Horizontal表示横向布局

ⅡRelativelyLayout 相对布局

1.必须能够确定每个控件的x、y、w、h
使用相对布局时不需要使用orientation

2.所有的布局类里面都维护一个LayoutParams extends MarginLayoutParams
用于管理当前这个布局子控件的布局
使用相对布局时不需要使用orientation

如何更改线性布局的方向
设置Orientation
Vertical表示纵向布局
Horizontal表示横向布局

所有的布局类里面都维护一个LayoutParams extends MarginLayoutParams
用于管理当前这个布局子控件的布局
Margin与Padding的区别
Margin指的是控件与页面的边缘的间距
而Padding指的是控件内部与空间边缘的间距

RelativelyLayout 相对布局
必须能够确定每个控件的x、y、w、h
使用相对布局时不需要使用orientation

在MarginLayout的基础上添加了对齐
当前这个控件和id为v1的控件右边对齐
android:layout_alignRight="@+id/v1"
android:layout_alignBottom="@+id/v1"

layout_centerHorizontal 横向对齐
layout_centerInParent 在父容器中对其居中对齐

ⅢConstrainLayout 约束布局

layout_constraintDimensionRatio 设置宽高比
layout_constraintDimensionRatio="w,1:2" 宽高比
layout_constraintDimensionRatio="h,1:2" 高宽比
2.xml文件中具体代码

    <View
    android:id="@+id/v1"
    android:background="@color/colorPrimary"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintVertical_bias="1.0"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintDimensionRatio="w,1:4"
    app:layout_constraintEnd_toStartOf="@id/v2"
    app:layout_constraintHorizontal_weight="1"
    />

    <View
        android:id="@+id/v2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintTop_toTopOf="@id/v1"
        app:layout_constraintBottom_toBottomOf="@id/v1"
        app:layout_constraintStart_toEndOf="@id/v1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintEnd_toStartOf="@id/v1"
        app:layout_constraintHorizontal_weight="1"
        />
<!--<View-->
    <!--android:layout_width="300dp"-->
    <!--android:layout_height="100dp"-->
    <!--android:layout_alignRight="@+id/v1"-->
    <!--android:layout_alignBottom="@+id/v1"-->
    <!--android:layout_weight="1"-->
    <!--android:background="@color/colorAccent" />-->

<!--<View-->
    <!--android:layout_width="100dp"-->
    <!--android:layout_height="100dp"-->
    <!--android:layout_weight="2"-->
    <!--android:background="@color/colorPrimaryDark" />-->
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料.第一类:属性值...
    HangChen阅读 10,434评论 0 24
  • 前言 在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来讲下Android开发中最常用...
    残月雨纷纷阅读 3,777评论 0 6
  • 线性布局 线性布局是程序中常见的布局方式之一,包括水平线性布局和垂直线性布局两种, 通过Android:orien...
    9545aa0ef865阅读 1,144评论 0 0
  • 布局【android:layout_centerHrizontal】Hrizontal表示水平【android:l...
    lfpwhy阅读 5,296评论 0 5
  • 今天整了好长时间文字版IP电话网络程序,可惜两台主机通过外网还是不能连通,心累
    李亚森阅读 1,098评论 0 1