这是我参与「第四届青训营 」笔记创作活动的第3天
FrameLayout
FrameLayout是 Android 所提供的布局方式里最简单的布局方式,它指定屏幕上的一块空白区域,在该区域填充一个单一对象。这些组件都将固定在屏幕的左上角,后放置的组件将覆盖先放置的组件。
LinearLayout
LinearLayout是Android中最常用的布局,该布局可以将放入其中的组件通过orientation属性选择水平或垂直放置。属性layout_weight给与组件不同的比重,空间分割的大小将由layout_weight的比重决定。
RelativeLayout
RelativeLayout是以一种让组件以相对于容器或者相对于容器中的另一个组件的相对位置进行放置的布局方式。该布局提供了一些常用的布局设置属性用于确定组件在视图中的相对位置。
| 属性 | 描述 |
|---|---|
| android:layout_above="@id/xxx" | 将控件置于给定 ID 控件之上 |
| android:layout_below="@id/xxx" | 将控件置于给定 ID 控件之下 |
| android:layout_toLeftOf="@id/xxx" | 将控件的右边缘和给定 ID 控件的左边缘对齐 |
| android:layout_toRightOf="@id/xxx" | 将控件的左边缘和给定 ID 控件的右边缘对齐 |
| android:layout_alignBaseline="@id/xxx" | 将控件的 baseline 与给定 ID 的baseline 对齐 |
| android:layout_alignTop="@id/xxx" | 将控件的上边缘和给定 ID 控件的上边缘对齐 |
| android:layout_alignBottom="@id/xxx" | 将控件的底边缘和给定 ID 控件的底边缘对齐 |
| android:layout_alignLeft="@id/xxx" | 将控件的左边缘和给定 ID 控件的左边缘对齐 |
| android:layout_alignRight="@id/xxx" | 将控件的右边缘和给定 ID 控件的右边缘对齐 |
| android:layout_alignParentLeft="true" | 将控件的左边缘和父控件的左边缘对齐 |
| android:layout_alignParentTop="true" | 将控件的上边缘和父控件的上边缘对齐 |
| android:layout_alignParentRight="true" | 将控件的右边缘和父控件的右边缘对齐 |
| android:layout_alignParentBottom="true" | 将控件的底边缘和父控件的底边缘对齐 |
| android:layout_centerInParent="true" | 将控件置于父控件的中心位置 |
| android:layout_centerHorizontal="true" | 将控件置于水平方向的中心位置 |
| android:layout_centerVertical="true" | 将控件置于垂直方向的中心位置 |
Relativelayout在布置tab页时特别好用,可以轻松做出这种效果,代码如下
<ImageButton
android:id="@+id/back"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@color/white"
android:src="@drawable/back" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:tabIndicatorColor="@color/black"
app:tabSelectedTextColor="@color/black"
app:tabTextColor="@color/gray" />
<ImageButton
android:id="@+id/addfriend"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginLeft="3dp"
android:background="@color/white"
android:src="@drawable/addfriend" />
</RelativeLayout>