Android kotlin CoordinatorLayout使用笔记(一)
Android kotlin CoordinatorLayout使用笔记(二)
Android kotlin CoordinatorLayout使用笔记(四)
前言
接前两篇,这篇将讲解下CollapsingToolbarLayout的用法,这个也是CoordinatorLayout的老伙伴了
CollapsingToolbarLayout
继承与FrameLayout,主要是用于实现折叠的AppBar效果,它需要放在AppBarLayout布局里面,并且作为AppBarLayout的直接子View。
app:title设置标题
app:collapsedTitleGravity 设置标题位置
位置字如其意,就不解释了
- top
- bottom
- left
- right
- center_vertical
- fill_vertical
- center_horizontal
- center
- start
- end
app:contentScrim 设置折叠时toolbar的颜色
默认是colorPrimary的色值
app:statusBarScrim 设置折叠时状态栏的颜色
默认是colorPrimaryDark的色值
app:layout_collapseParallaxMultiplier 设置视差
类型是Float,范围0~1
app:layout_collapseMode折叠模式
- none 无
- pin 固定模式,在折叠的时候最后固定在顶端
- parallax 视差模式,在折叠的时候会有个视差折叠的效果
使用示例:让图片折叠,让toolbar固定
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="title"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/bg"
android:scaleType="fitXY"
android:layout_marginTop="46dp"
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:padding="10dp"
android:text="这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本这是一个长文本"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
结语
下一篇就是总篇了,CoordinatorLayout整体的使用了,敬请期待
网友评论