Play Appbarlayout, more cool top bar

Source: Internet
Author: User

My book Sync release: Play Appbarlayout, cooler top Bar

The previous article, "Coordinatelayout is so simple to use" to CoordinateLayout explain the use of, and today we will talk about the use of several View : AppBarLayout , CollapsingToolbarLayout and Toolbar . Suddenly appeared 3 strange View , is not feel very flustered ~, a lot of people have written the use of these several layouts, but they do not have a specific explanation of the role of each view and how to use, I see a lot of articles are on the one AppBarLayout , CollapsingToolbarLayout and Toolbar Write to a layout inside go, and then a layout property to say, suddenly feel good chaos, this article is starting from toolbar, finally let you take these 3 view completely master down!

In fact, these three view is for our common use ActionBar , is for our app's top bar to play a variety of tricks ~ we look down, see what they put our app's "Top bar" play out what tricks!

1 Toolbar

Toobar is mainly used to replace Actionbar, in other words, Actionbar can do, toolbar can do. If you are familiar with the use of Actionbar, you will find toolbar very simple to use. OK, since is the replacement, of course, when using toolbar, you have to go to the Actionbar to hide it ~

There are many ways to hide actionbar, which can be hidden by code, or by the way we configure files, mainly by means of configuration files. Add the following two lines to the apptheme tag in our Styles.xml file:

<item name="windowActionBar">false</item><item name="windowNoTitle">true</item>

Of course, you can also create a new <style> label, add the above two lines of code, and use this new tag as <application> the theme . You can also choose how to set the parent of the Apptheme to Theme.AppCompat.Light.NoActionBar . Many ways, you can choose freely.

The next step is to put the toolbar into the layout file (nothing to explain):

  <android.support.v7.widget.Toolbar        android:id="@+id/toolbar"        android:layout_width="match_parent"        android:background="?attr/colorPrimary"        android:layout_height="?android:attr/actionBarSize"  />

Finally, Toobar is used as "ActionBar".

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        toolbar.setTitle("这里是Title");        toolbar.setSubtitle("这里是子标题");        toolbar.setLogo(R.drawable.icon);         setSupportActionBar(toolbar);

Toolbar can set the logo, title, sub-title and so on ~ There are many other settings, to play slowly, do not mention here ~. Of course, you can also set these in the layout file, in the layout file settings do not write, Hongyang great God has a blog written very good "Android 5.x Theme and ToolBar combat" can go to reference.

Look at the effect:

If toolbar is only used to replace the previous actionbar, it is too creative! There's absolutely no need to replace them, because they're all the same, and they don't make us feel more comfortable using them than actionbar. Why to replace it, there should always be his reason: Actionbar is fixed at the top, and can not move, I think this is the biggest bad, and our toolbar can let us casually placed, we can bring a lot of flexibility and effect!

As you can see, toolbar is not enough to see, it's not complicated at all. Next we continue to learn to set a layer of toolbar above the parent view, so that toolbar more interactive.

2 Appbarlayout

The appbarlayout inherits from the LinearLayout, and the layout direction is vertically oriented. So you can use it as a vertical layout linearlayout. Appbarlayout is the concept of material design on linearlayou, which allows you to customize what happens to the child view within a scrollable view when the scrolling gesture changes.

Please note : One of the scrollable viewmentioned above can be understood as a scrollview. How do you understand the above? That is, when a scrollview is scrolled, you can customize which actions your "top bar" should perform (such as scrolling along, keeping it motionless, etc.). What is the movable view of a movable view? This is your own designation! How to specify, we say behind.

2.1 Appbarlayout The action of a child view

The inner sub-view app:layout_scrollFlags app:layout_scrollFlags lets you set which actions to perform by adding the actions in the layout. The following are the differences:

(1) scroll : The view with the value set is scroll moved along with the scrolling event.

What do you mean? Simply put, it is when the specified ScrollView scrolls, the view also follows the scroll, as if the view belonged to this scrollview.

A GIF is sufficient to illustrate:

The corresponding layout file

<android.support.design.widget.appbarlayout  android:layout_width  = "match_parent"  android:layout_height  =;         <android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android: Layout_width="Match_parent"android:layout_height="? android:attr/actionbarsize" android:background="? attr/colorprimary"app:layout_scrollflags="scroll" />                                                            </android.support.design.widget.AppBarLayout>

(2) enterAlways : The value is set to enterAlways a view, and when ScrollView scrolls down, the view scrolls straight down. Regardless of whether the scrollview is rolling.

Watch a cartoon (Y (^o^) y) (Toolbar height: ?android:attr/actionBarSize , app:layout_scrollFlags="scroll|enterAlways" ):

(3) exitUntilCollapsed : The value is set to exitUntilCollapsed the view, when the view will gradually "fade", will continue to slide upward, until the remaining height to its minimum height, and then respond to scrollview internal sliding events.

How do you understand it? Simple explanation: When the ScrollView up, the first is the view of the sliding event "take away", by the view to perform the slide, until the minimum height to slide, the slide event "back", let the ScrollView interior to slide. Take a look at a gif (the height of the image is relatively large: 200DP, and the minimum height is set to ?android:attr/actionBarSize , app:layout_scrollFlags="scroll|exitUntilCollapsed" ):

(4) enterAlwaysCollapsed : Is enterAlways an additional option, generally with the use of, enterAlways it refers to the view in the downward "appearance" when the first is the enterAlways effect, when the height of the view reached the minimum height, the view is not to scroll down temporarily, Until ScrollView slides to the top and no longer slides, the view continues to slide down until it ends at the top of the view.

Let's have a gif. (the height of the image is relatively large: 200DP, and the minimum height is set to ?android:attr/actionBarSize , app:layout_scrollFlags="scroll|enerAlways|enterAlwaysCollapsed" ):

2.2 Associating Appbarlayout with ScrollView

Said before repeatedly said "when the ScrollView occurs when rolling", then how to Appbarlayout and ScrollView related to it? We notice that the movement between Appbarlayout and ScrollView is "interdependent", and this is not what we learned in our last article, "Coordinatelayout is so simple to use"? Take ScrollView and Appbarlayout as Coordinatelayout's child view, then write a behavior, In this behavior, judging the current operation is supposed to keep ScrollView time under appbarlayout (that is, just change the position of the appbarlayout can slide together), Or should let ScrollView internal rolling and not let appbarlayout position change and so on these requirements, can be processed in the behavior inside. You can write behavior for your scrollview. However, we see our appbarlayout pre-function is more complex, if we ourselves to define such an effect, the code is very complex, but also to consider a lot of aspects, fortunately, Android help us to write, we directly use it, This scrollview is Nestedscrollview, please note that it does not inherit ScrollView, it inherits Framelayout, but the effect it implements can be seen as ScrollView.

Put the Nestedscrollview into our layout file inside can ~ ~ ~ ~, very convenient ~

<android.support.v4.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:layout_behavior="@string/appbar_scrolling_view_behavior">       <!--将你的内容放在这里-->    </android.support.v4.widget.NestedScrollView>

Notice there is a property: app:layout_behavior="@string/appbar_scrolling_view_behavior" , it is the specified behavior, the appbar_scrolling_view_behavior corresponding class name is: android.support.design.widget.AppBarLayout$ScrollingViewBehavior interested can go to analyze the source code.

OK, we will use Appbarlayout now ~ is not found to use so easy! next we put the rest CollapsingToolbarLayout to "digestion" Off!

3 Collapsingtoolbarlayout

CollapsingToolbarLayoutis used for Toolbar re-packaging ViewGroup , mainly for the implementation of folding (in fact, looks like stretching ~) app Bar effect. It needs to be placed AppBarLayout inside the layout and as AppBarLayout a direct child View . CollapsingToolbarLayoutmainly includes several functions ( referring to the content of the official website, slightly own understanding to explain ):

(1) Folding title (collapsing title): When the layout content is displayed, the title is the largest, but as the view moves out of the top of the screen, the title becomes smaller. You can set the title by calling the Settitle function.

(2) Contents gauze (content Scrim): Depending on whether the scroll position reaches a threshold, to determine whether the view "cover gauze". A picture of the gauze can be set by Setcontentscrim (drawable).

(3) Status bar gauze (status bar scrim): Depending on whether the scroll position reaches a threshold to determine whether to "cover gauze" on the status bar, you can setStatusBarScrim(Drawable) set the gauze picture, but only on the LOLLIPOP device has a role.

(4) Parallax scroll view (Parallax scrolling children): The sub view can choose to follow the scroll in the current layout in a "parallax" fashion. (PS: In fact, let this view scroll faster than the other normal scrolling view speed slightly slower ). Set the layout parameter app:layout_collapseMode toparallax

(5) Pin the Sub view position (Pinned position Children): Child view can choose whether to fix the position on the global space, which is very useful for toolbar, because when the layout is moving, it can be toolbar fixed position without being affected by the movement. will be app:layout_collapseMode set to pin .

After understanding these concepts, let's take a look at the layout bar ~

<?xml version= "1.0" encoding= "Utf-8"?><android.support.design.widget.coordinatorlayout  Span class= "Hljs-attribute" >xmlns:android  = "http://schemas.android.com/apk/res/ Android " xmlns:app  ="/http " Schemas.android.com/apk/res-auto " android:layout_width  =" match_parent " android:layout_height  =  "match_parent" ;     <android.support.design.widget.appbarlayout  android:layout_width  = "match_parent"  android:layout_height  =" wrap_content " android:theme  =" @style/themeoverlay.appcompat.dark.actionbar " ;         <android.support.design.widget.collapsingtoolbarlayout              android:layout_width  = "match_parent"             android:layout_height  = "wrap_content"  app:expandedtitlemarginend  = "64DP"  
     
      app:expandedtitlemarginstart 
      = "48DP"  app:layout_scrollflags  = "scroll|exituntilcollapsed"              <imageview  android:i D  = "@+id/main.backdrop"  android:layout_w Idth  = "wrap_content"  android:layout_heig HT  = "300DP"  android:scaletype  =
     
       "Centercrop" 
      android:src  =" @drawable/material_img " app:layout_collapsemode  = "parallax" />             <android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android: Layout_width="Match_parent"android:layout_height="? android:attr/actionbarsize" App:layout_collapsemode="pin" />                                                                        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>    <android.support.v4.widget.nestedscrollview  android:layout_width  = "match_parent"  android:layout_height  =" wrap_content " android:paddingtop  =" 50DP "  App:layout_behavior  = "@string/appbar_scrolling_view_behavior" ;         <TextViewandroid:layout_width="Match_parent"android:layout_height= "Wrap_content" android:text= "@string/my_txt"android:textsize="20sp" />                                                    </android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout>

The above all understand, every strange property is said Oh, forget the words look back, a little explanation, the picture is set to have parallax sliding, toolbar set to fixed, in addition, collapsingtoolbarlayout will enlarge and reduce the title, Let's see how it works ~

If you want the status bar to be transparent during the drag process, you can add app:statusbarscrim= "@android: Color/transparent" in Collapsingtoolbarlayout, and call GetWindow () in OnCreate. Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) set the status bar to transparent just fine ~

SOURCE, please consultation fee: http://download.csdn.net/detail/huachao1001/9538934

Play Appbarlayout, more cool top bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.