内容
Ⅰ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" />-->