The Collapsingtoolbarlayout function is to provide a collapsible toolbar, which inherits to Framelayout, sets it to Layout_scrollflags, It can control the controls contained in Collapsingtoolbarlayout (such as: ImageView, Toolbar) to make corresponding Scrollflags scrolling events when responding to Layout_behavior events (removing the screen or pinning it to the top of the screen )。
Using collapsingtoolbarlayout:
<android.support.design.widget.AppBarLayout android:layout_width="match_parent"Android:layout_height="256DP"android:fitssystemwindows="true"> <android.support.design.widget.CollapsingToolbarLayout Android:id="@+id/collapsing_toolbar_layout"Android:layout_width="match_parent"Android:layout_height="match_parent"App:contentscrim="#30469b"App:expandedtitlemarginstart="48DP"App:layout_scrollflags="scroll|exituntilcollapsed"> <ImageView android:layout_width="match_parent"Android:layout_height="match_parent"Android:scaletype="Centercrop"android:src="@mipmap/BG"App:layout_collapsemode="Parallax"App:layout_collapseparallaxmultiplier="0.7"/> <Android.support.v7.widget.Toolbar Android:id="@+id/toolbar"Android:layout_width="match_parent"Android:layout_height="? attr/actionbarsize"App:layout_collapsemode="Pin"/> </android.support.design.widget.CollapsingToolbarLayout> </ANDROID.SUPPORT.DESIGN.WIDGET.APPB Arlayout>
We set up a ImageView and a toolbar in the Collapsingtoolbarlayout. And put this collapsingtoolbarlayout in the appbarlayout as a whole.
1, in the collapsingtoolbarlayout:
We set the Layout_scrollflags: about its value I'll say it again here:
- Scroll-You must set this if you want to scroll.
- Enteralways-Implements the quick return effect, and immediately displays the view (such as toolbar) when you move down.
- Exituntilcollapsed-Shrinks the view when scrolling up, but can be fixed toolbar always on top.
- Enteralwayscollapsed-When your view has set the MinHeight property and uses this flag, your view can only enter at the minimum height, and only when the scrolling view reaches the top will it expand to full height.
Some properties are also set, briefly stating:
- Contentscrim-Sets the background color when fully collapsingtoolbarlayout folded (shrunk).
- Expandedtitlemarginstart-Sets the distance to the left of the title when the extension is set (not yet contracted).
No time for expansion.
2. In the ImageView control:
We have set up:
- Layout_collapsemode (folding mode)-There are two values:
- Pin-When set to this mode, toolbar can also remain on the screen when Collapsingtoolbarlayout is fully contracted.
- Parallax-When set to this mode, when content scrolls, the view (such as ImageView) in Collapsingtoolbarlayout can also scroll simultaneously, realizing the parallax scrolling effect, usually and layout_ Collapseparallaxmultiplier (set parallax factor) with.
- Layout_collapseparallaxmultiplier (parallax Factor)-sets the parallax scrolling factor with a value of: 0~1.
3. In the Toolbar control: we set the Layout_collapsemode (folding mode): PIN. Comprehensive analysis: When the Layout_behavior control is set to respond to the Layout_scrollflags event in Collapsingtoolbarlayout, the ImageView will have a parallax effect to scroll up to remove the screen. When you start folding, the background color of the collapsingtoolbarlayout (that is, the background color of toolbar) becomes the background color we set, and the toolbar is always fixed at the top. Effect: NOTE: When using Collapsingtoolbarlayout, the title must be set to Collapsingtoolbarlayout and will not be displayed on toolbar. namely: Mcollapsingtoolbarlayout.settitle (""); The font color of the changed title: the title Color of the expansion: Mcollapsingtoolbarlayout.setexpandedtitlecolor (); Color of title when displayed on toolbar after shrinking: Mcollapsingtoolbarlayout.setcollapsedtitletextcolor (); The color of the excessive change in fact Collapsingtoolbarlayout has helped us do well, it will automatically over, for example, we put the title color after the contraction to green, the effect is not recorded well, anyway the effect came out. Let's take a look at how the code is implemented: Layout files:
<android.support.design.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"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent"Android:layout_height="256DP"android:fitssystemwindows="true"> <android.support.design.widget.CollapsingToolbarLayout Android:id="@+id/collapsing_toolbar_layout"Android:layout_width="match_parent"Android:layout_height="match_parent"App:contentscrim="#30469b"App:expandedtitlemarginstart="48DP"App:layout_scrollflags="scroll|exituntilcollapsed"> <ImageView android:layout_width="match_parent"Android:layout_height="match_parent"Android:scaletype="Centercrop"android:src="@mipmap/BG"App:layout_collapsemode="Parallax"App:layout_collapseparallaxmultiplier="0.7"/> <Android.support.v7.widget.Toolbar Android:id="@+id/toolbar"Android:layout_width="match_parent"Android:layout_height="? attr/actionbarsize"App:layout_collapsemode="Pin"/> </android.support.design.widget.CollapsingToolbarLayout> </ANDROID.SUPPORT.DESIGN.WIDGET.APPB Arlayout> <LinearLayout android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"App:layout_behavior="@string/appbar_scrolling_view_behavior"> <Android.support.v7.widget.RecyclerView Android:id="@+id/recyclerview"Android:layout_width="match_parent"Android:layout_height="match_parent"Android:scrollbars="None"/> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
Code files:
Toolbar Mtoolbar =(Toolbar) Findviewbyid (R.id.toolbar); Setsupportactionbar (Mtoolbar); Getsupportactionbar (). setdisplayhomeasupenabled (true); Mtoolbar.setnavigationonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {onbackpressed (); } }); //use Collapsingtoolbarlayout must set the title to Collapsingtoolbarlayout, set to Toolbar will not displayCollapsingtoolbarlayout mcollapsingtoolbarlayout =(collapsingtoolbarlayout) Findviewbyid (r.id.collapsing_toolbar_layout); Mcollapsingtoolbarlayout.settitle ("Collapsingtoolbarlayout"); //modifying font colors with CollapsingtoolbarlayoutMcollapsingtoolbarlayout.setexpandedtitlecolor (Color.White);//Set the font color when it's not shrinkingMcollapsingtoolbarlayout.setcollapsedtitletextcolor (Color.green);//set the color of the font on toolbar after shrinking
Source: http://download.csdn.net/detail/u010687392/8906931
Android Material Design Collapsingtoolbarlayout use