Android Support Design Library--snackbar

Source: Internet
Author: User

Introduced:

  Snackbaris Android Support Design Library a control supported by the library for prompting some key information under the interface, unlike Toast, where Snackbar allows the user to swipe right to dismiss it, while also allowing an action to be set in Snackbar When the user clicks on the Snackbar inside the button, can carry on some operation, therefore, the function is absolutely very powerful.

The official recommendation CoordinatorLayout( Android Support Design Library is to use a control supported by another library) to accommodate. Because using this control, you can guarantee that the Snackbar user can swipe right to exit.

1. Basic function: Eject Snackbar from bottom

To define a container for Snackbar in an XML file: Coordinator container:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <Button Android:onclick= "Createsnackbar"Android:text= "Snackbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/> <android.support.design.widget.CoordinatorLayout Android:id= "@+id/container"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"/></relativelayout>

Activity shows Snackbar (it is used like a toast, it pops up after three seconds, or the user slides right, it slides out)

 Public classSnackbartestextendsappcompatactivity {coordinatorlayout container; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_snackbar_test); Container=(coordinatorlayout) Findviewbyid (R.id.container); }     Public voidCreatesnackbar (View v) { snackbar.make (container, "Snackbar Maker" , Snackbar.length_long). Show (); }}

The first container parameter of the Snackbar can also be a linearlayout or other container, and when the snackbar pops up, it does not slide the effect to the right.

2. Add a button and define the Click event
Snackbar.make (Container, "Snackbar with Button", Snackbar.length_long)
. Setaction ("Action", New View.onclicklistener () {
@Override
public void OnClick (View v) {
//
}
})
. Setactiontextcolor (Color.Red)
. Show ();
3. Set Snackbar callback OnShow and ondismissed

Custom Snackbar.callback Class

 Public classMycallbackextendssnackbar.callback{@Override Public voidOndismissed (Snackbar Transientbottombar,intevent) {        Super. ondismissed (Transientbottombar, event); if(event = = Dismiss_event_swipe | | event = =Dismiss_event_timeout|| event = =dismiss_event_consecutive) {}} @Override Public voidOnshown (Snackbar sb) {Super. Onshown (SB); //    }}

Snackbar Setting Callback ()

Snackbar.make (Container, "Snackbar maker", Snackbar.length_long)        . Setcallback (new  Mycallback ())        . Show ();

Type of supplemental Snackbar disappearance:

/**indicates the Snackbar was dismissed via a swipe.*/ Public Static Final intDismiss_event_swipe = 0;/**indicates that the Snackbar is dismissed via an action click.*/ Public Static Final intDismiss_event_action = 1;/**indicates that the Snackbar is dismissed via a timeout.*/ Public Static Final intDismiss_event_timeout = 2;/**indicates that the Snackbar is dismissed via a call to {@link#dismiss ()}.*/ Public Static Final intDismiss_event_manual = 3;/**indicates that the Snackbar is dismissed from a new Snackbar being shown.*/ Public Static Final intDismiss_event_consecutive = 4;

Follow-up Review supplement:

http://www.jianshu.com/p/cd1e80e64311

Coordinator Container:

Http://www.jianshu.com/p/488283f74e69

Http://www.cnblogs.com/itgungnir/p/6210803.html

Android Support Design Library--snackbar

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.