Android学习系列Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout使用小结
来源:互联网 发布:淘宝叫人拍注意事项 编辑:程序博客网 时间:2024/06/06 00:02
Android学习系列Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout使用小结
版权声明:本文为博主原创文章,码字不易,转载请注明原文地址,谢谢。
上面几个控件,相信大家已经耳熟能详,是基于MD风格的Android Design Support Library里面所包含的控件。
添加依赖:
- 1
- 1
Toolbar小结
使用Toolbar,首先要关闭actionbar。
方法1:
使得Activity的主题继承Theme.AppCompat.NoActionBar
方法2:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
然后在清单文件中的application 添加 android:theme=”@style/AppTheme”即可
布局:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
使用actionBarSize默认高度,以及colorPrimary作为背景色,可自选。
使用:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
setNavigationIcon,setOnMenuItemClickListener必须放在setSupportActionBar之后才有效果,setLogo,setSubtitle,setTitle,必须放在setSupportActionBar之前才有效果。
menu点击:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
AppBarLayout小结:
AppBarLayout是一个垂直方向的linearLayout,包含了对滑动手势的处理。我们可以使用app:layout_scrollFlags这样的标签来设置滑动的行为表现。
使用AppBarLayout需要注意下面几个要点:
首先,AppBarLayout必须作为CoordinatorLayout的直接子View
其次,在AppBarLayout里面必须包含一个ToolBar
最后,在CoordinatorLayout里面可以添加那些可以滑动的组件,例如RecyclerView
布局结构:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
解析:
一:因为包含toolBar
android:theme=”@style/ThemeOverlay.AppCompat.Dark.ActionBar”
二: 设置滑动隐藏
app:layout_scrollFlags=”scroll|enterAlways”
enterAlways:
一旦向上滚动这个view就可见。
enterAlwaysCollapsed:
顾名思义,这个flag定义的是何时进入(已经消失之后何时再次显示)。假设你定义了一个最小高度(minHeight)同时enterAlways也定义了,那么view将在到达这个最小高度的时候开始显示,并且从这个时候开始慢慢展开,当滚动到顶部的时候展开完。
exitUntilCollapsed:
同样顾名思义,这个flag时定义何时退出,当你定义了一个minHeight,这个view将在滚动到达这个最小高度的时候消失。
三:滑动组件的动画,满一屏才有效果。
app:layout_behavior=”@string/appbar_scrolling_view_behavior”
效果:
滑动隐藏AppBarLayout
CoordinatorLayout小结:
CoordinatorLayout继承自framelayout,我们相当于在framelayout上做一些操作
添加Floating Action Button:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
app:backgroundTint设置背景颜色
效果:
FloatingActionButton随着Recylerview的滑动显示和隐藏:
方法一:
监听recylerview或者scrollview的滑动事件,判断是上滑还是下滑,
1:定义变量private int mScrollThreshold=0;
2:监听滑动
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
这是比较容易理解,比较容易实现的方法。
方法二:
自定义ScrollAwareFABBehaviorDefault:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
在布局中引用:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
其他方式借鉴:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0407/4126.html
CollapsingToolbarLayout
CollapsingToolbarLayout能让我们做出更高级的动画,比如在里面放一个ImageView,然后在它折叠的时候渐渐淡出。同时在用户滚动的时候title的高度也会随着改变。
效果实例:
布局结构:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
解析:
app:layout_collapseMode=”pin”来确保Toolbar在view折叠的时
候仍然被固定在屏幕的顶部。使用app:layout_collapseMode=”parallax”(以及使用
app:layout_collapseParallaxMultiplier=”0.7″来设置视差因子)来实现视差滚动效果(比如
CollapsingToolbarLayout里面的一个ImageView),这中情况和CollapsingToolbarLayout的
app:contentScrim=”?attr/colorPrimary”属性一起配合更完美。
Java设置:
- 1
- 2
- 1
- 2
解析:当你让CollapsingToolbarLayout和Toolbar在一起使用的时候,title会在展开的时候自动变得 大些,而在折叠的时候让字体过渡到默认值。必须注意,在这种情况下你必须在CollapsingToolbarLayout上调用setTitle(), 而不是在Toolbar上。
- Android学习系列Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout使用小结
- Android学习系列Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout使用小结
- Android学习系列Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout使用小结
- CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar用法小结
- Android Material Design(5) CoordinatorLayout,AppBarLayout,Toolbar,CollapsingToolbarLayout的使用
- CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar
- CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout、Toolbar混合使用经验
- JuheNews系列之二 · ToolBar+AppBarLayout+CoordinatorLayout+CollapsingToolbarLayout
- CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,Toolbar,NestedScrollView,RecyclerView结合使用
- CoordinatorLayout、AppBarLayout、 Toolbar、CollapsingToolbarLayout、NestedScrollView 综合使用,详细解析
- Android Material 之CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout、Toolbar 等的说明和使用
- Android MD: CoordinatorLayout AppBarLayout CollapsingToolbarLayout
- CoordinatorLayout,CollapsingToolbarLayout,AppBarLayout简单使用
- Material Design学习:CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout
- AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
- CoordinatorLayout、AppBarLayout、TableLayout、CollapsingToolbarLayout、NestedScrollView的使用
- UI之CoordinatorLayout、AppbarLayout、CollapsingToolbarLayout的使用
- RecyclerView与CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout 解决上滑到toolbar卡顿
- 存储过程调用存储过程
- 在Android Studio中设置序列化版本号
- dp——洛谷P1281 书的复制
- eclipse下安装tomcat
- XMind的智能截图功能
- Android学习系列Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout使用小结
- DOS bat批量处理 文本操作
- Vijos P1571 笨笨的导弹攻击【最长上升子序列+DP】
- Linux下的串口编程
- 单链表排序
- 多核优化,使用linux affinity 将进程,线程,中断指定到对应的cpu运行,用ftrace查看消耗时间
- 手动提交表单的遍历提取数据
- CSS的单位
- tool——toolofna