设置视图间距有两种方式:
layout_margin :它指定了当前视图与周围平级视图之间的距离。包括 layout_marginLeft、layout_marginTop、layout_marginRight、layout_marginBottom
padding:它指定了当前视图与内部下级视图之间的距离。包括padding、paddingLeft、paddingTop、paddingRight、paddingBottom等
设置视图的对齐方式:
layout_gravity : 它指定了当前视图相对于上级视图的对齐方式。
gravity :它指定了下级视图相对于当前视图的对齐方式。
常用布局方式:
1.LinearLayout线性布局:
线性布局内部的各视图有两种排列方式:
orientation属性值为horizontal时,内部视图在水平方向从左往右排列
orientation属性值为vertical时,内部视图在水平方向从上往下排列
默认水平方向排列
layout_weight : 线性布局的下级视图各自拥有多大比例的宽高(直接在下级视图设置)
2.RelativeLayout 相对布局:
相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:
与该视图自身平级的视图
该视图的上级视图(也就是它所归属的RelativeLayout)
如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/purple"
android:layout_centerInParent="true"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/purple_200"></View>
<View
android:id="@+id/green"
android:layout_alignLeft="@+id/purple"
android:layout_below="@+id/purple"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FF00FFAA"></View>
</RelativeLayout>
3.GridLayout 网格布局:
网格布局支持多行多列的表格排列
网格布局默认从左往右、从上到下、它新增了两个属性:
columnCount属性,它指定了网格的列数,即每行能放多少个视图;
rowCount属性,它指定了网格的行数,即每列能放多少个视图;
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:gravity="center"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ffcccc"
android:text="浅红色"
android:textSize="17sp"
></TextView>
<TextView
android:gravity="center"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ffaa00"
android:text="橙色"
android:textSize="17sp"
></TextView>
<TextView
android:gravity="center"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ff00ff"
android:text="紫色"
android:textSize="17sp"
></TextView>
<TextView
android:gravity="center"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ff00ff00"
android:text="绿色"
android:textSize="17sp"
></TextView>
</GridLayout>
4.ScrollView滚动视图有两种:
ScrollView它是垂直方向的滚动视图;垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content(高度为包括内容,当内容的高度超出ScrollView指定的高度时可以滚动)
HorizonalScrollView 它是水平方向的滚动视图;水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent
<!-- 水平方向的线性布局 -->
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="@color/purple_200"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:background="@color/purple_200"
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="第一个"></TextView>
<TextView
android:background="@color/teal_200"
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="第二个"></TextView>
</LinearLayout>
</HorizontalScrollView>
<!-- 垂直方向的线性布局 -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@color/teal_200"
android:text="第三个"></TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@color/purple_500"
android:text="第四个"></TextView>
</LinearLayout>
</ScrollView>