Android Design Support Library use detailed
Google in the 2015 IO Conference, brought us more detailed material design specifications, but also brought us a new Android design support Library, in this support repository, Google provides us with a more standard MD design-style control. Most importantly, the Android Design Support Library is more compatible and can be directly backwards compatible to Android 2.2. It has to be said to be a conscience.
Using the support library is simple:
To add a reference:
compile ‘com.android.support:design:22.2.0‘
Let's take a look at the basic use of these new controls, starting with the simplest controls.
Snackbar
Snackbar provides a lightweight control between toast and Alertdialog, which makes it easy to provide prompt and action feedback for messages.
The use of Snackbar is basically the same as the use of toast:
Snackbar.make(view, Snackbar comes out, Snackbar.LENGTH_LONG) .setAction(Action, new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText( MainActivity.this, Toast comes out, Toast.LENGTH_SHORT).show(); } }).show();
It is important to note that here we take the first parameter as the datum element of the Snackbar display, and the set action can be set more than one.
The displayed effect is similar to the following:
vcu1w/ Ejujwvcd4ncjxwpmh0dha6ly9kzxzlbg9wzxiuyw5kcm9pzc5jb20vcmvmzxjlbmnll2fuzhjvawqvc3vwcg9ydc9kzxnpz24vd2lkz2v0l1nuywnrymfylmh 0bww8l3a+dqo8cd48aw1nigfsdd0= "Write a picture here describing" src= "/uploadfile/collfiles/20150605/20150605084858146.png" title= "\"/>
Textinputlayout
Textinputlayout as a parent container control, wrapping the new edittext. Typically, a separate edittext will hide the hint hint after the user enters the first letter, but now you can use Textinputlayout to encapsulate the edittext, prompting the message to become a edittext that appears on top of the floating Label so that users will always know what they are typing now. At the same time, if you add more monitoring to EditText, you can add more floating label to it.
Let's look at this with a textinputlayout:
It is important to note that he is a edittext, and cannot be used alone.
In the code, we set it to listen:
final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.til_pwd); EditText editText = textInputLayout.getEditText(); textInputLayout.setHint(Password); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (s.length() > 4) { textInputLayout.setError(Password error); textInputLayout.setErrorEnabled(true); } else { textInputLayout.setErrorEnabled(false); } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } }); }
This: The display effect is as follows:
When you enter:
It is important to note that the color of the textinputlayout comes from the color of the coloraccent in the style:
#1743b7
Below we give the instructions on the Google API doc to learn more about how to use Textinputlayout:
Http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
Floating Action Button
The floating action button is a circular button that is responsible for displaying the basic operation of the interface. The Floatingactionbutton in the Design library implements a default color for the hover action button in the theme coloraccent, like this:
Floatingactionbutton--fab is very simple to use, you can specify in the reinforced framelayout inside the--coordinatorlayout, this we will back again.
For the use of fab, you can think of it as a button.
You can specify the location of a layout_gravity by specifying it.
Again, you can specify the anchor point for the location by specifying anchor:
In addition to the general size of the hover operation button, it also supports mini size (fabsize= "Mini"). Floatingactionbutton inherits from ImageView, you can use any method of android:src or ImageView, such as setimagedrawable () To set the icon inside the Floatingactionbutton.
Http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
Tablayout
The tab swipe switch view is not a new concept, but Google has provided full support for the first time in the Help library, and the Design Library's tablayout has implemented a fixed tab-the width of the view is evenly distributed, A scrollable tab is also implemented-the view width is not fixed and can be scrolled horizontally. Tabs can be added dynamically in your program:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.addTab(tabLayout.newTab().setText(tab1)); tabLayout.addTab(tabLayout.newTab().setText(tab2)); tabLayout.addTab(tabLayout.newTab().setText(tab3));
But most of the time we will not use this, usually the sliding layout will be used in conjunction with Viewpager, so we need Viewpager to help:
mViewPager = (ViewPager) findViewById(R.id.viewpager); // 设置ViewPager的数据等 setupViewPager(); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager);
By a word setupwithviewpager, we put Viewpager and tablayout together.
Http://developer.android.com/reference/android/support/design/widget/TabLayout.html
Navigationview
Navigationview is very important in MD design, before Google also proposed using Drawerlayout to implement the navigation drawer. This time, in the support library, Google provided the Navigationview to implement the navigation menu interface, so the new navigation interface can be written like this:
The most important of these are the two properties:
App:headerlayout
App:menu
With these two properties, we can easily specify the head layout and menu layout of the Navigation interface:
One of the top layouts is the header layout specified by App:headerlayout:
With the menu layout below, we can automatically generate it directly with the menu content without requiring us to specify the layout:
You can get the callback event that the element is selected by setting a Onnavigationitemselectedlistener and using its setnavigationitemselectedlistener (). It provides you with clicked menu elements that allow you to handle selection events, change check box status, load new content, close the navigation menu, and any other actions you want to do. For example, this:
private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(true); mDrawerLayout.closeDrawers(); return true; } }); }
As you can see, Google's encapsulation of these things is very easy to use.
Appbarlayout
Appbarlayout, like its name, takes the components of the container class all as AppBar. Like this:
Here is to put toolbar and tablayout into the appbarlayout, let them as a whole as appbar.
Http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html
Coordinatorlayout
Coordinatorlayout is an enhanced framelayout that is newly added. In Coordinatorlayout, we can do a lot of new things on the basis of framelayout.
Floating View
A new feature of MD is the addition of a lot of hover view, like the floating Action Button we said earlier. We can place the fab anywhere, just by:
android:layout_gravity=end|bottom
To specify the location to display. It also provides an anchor point for you to set the display coordinates for Layout_anchor:
app:[email protected]/appbar
Create a scrolling
Coordinatorlayout can be said to be the priority of this support library update. It controls the layout of touch events between sub-view from another level, and many of the controls in the Design library take advantage of it.
A good example is when you add Floatingactionbutton as a child view into coordinatorlayout and pass coordinatorlayout to Snackbar.make (), On devices with 3.0 and more, the Snackbar does not appear on top of the hover button, but rather Floatingactionbutton uses the callback method provided by Coordinatorlayout. The snackbar automatically moves up when the animation effect enters, and returns to its original position when the Snackbar animation disappears, without additional code.
The official example is a good illustration of this:
One of the components that can be scrolled, such as Recyclerview, ListView ( Note here, seems to support only Recyclerview, ListView, if you use a scrollview, it is ineffective ). If:
1. Set the Layout_behavior for this scrollable component
2. Set the layout_scrollflags for another control
Then, when you set the Layout_behavior control to slide, it triggers a change in the state of the control that has the layout_scrollflags set.
There are several options for setting the Layout_scrollflags:
Scroll: All view that wants to scroll out of the screen needs to be set this flag-the view without setting this flag will be pinned to the top of the screen. Enteralways: This flag allows any downward scrolling to cause the view to become visible, enabling quick "return mode". Enteralwayscollapsed: When your view has already set the MinHeight property and use this flag, your view can only be entered at the minimum height, and only when the scrolling view reaches the top will it expand to full height. Exituntilcollapsed:this flag causes the view to scroll off until it was ' collapsed ' (its minheight) before exiting.
It is important to note that the following two modes are basically only used in Collapsingtoolbarlayout, and the first two modes are basically used together, that is to say, the use of these flags is basically fixed.
For example, in our previous example, this is the pattern:
app:layout_scrollFlags=scroll|enterAlways
PS: All view using scroll flag must be defined in front of a view that does not use scroll flag to ensure that all view exits from the top, leaving a fixed element.
Http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html
Collapsingtoolbarlayout
Collapsingtoolbarlayout provides a collapsible toolbar, which is also an effect in Google +, photos. Google has made it a standard control that makes it easier for everyone to use.
Let's look at an example here:
We placed a imageview and a toolbar in the Collapsingtoolbarlayout. And put this collapsingtoolbarlayout in the appbarlayout as a whole. In Collapsingtoolbarlayout, we set up ImageView and a toolbar layout_collapsemode respectively.
The collapsingtoolbarlayout app:layout_collapsemode= "pin" is used here to ensure that the toolbar is still pinned to the top of the screen when the view is folded. When you let collapsingtoolbarlayout and toolbar work together, the title will automatically become larger when it is expanded, while the font transitions to the default value when folded. It must be noted that in this case you must raise the Settitle () on the collapsingtoolbarlayout instead of on the toolbar.
In addition to the fixed view, you can also use the app:layout_collapsemode= "parallax" (as well as using the app:layout_collapseparallaxmultiplier= "0.7" To set the Parallax factor) to achieve the Parallax scrolling effect (such as a imageview in Collapsingtoolbarlayout), which is the case and Collapsingtoolbarlayout app:contentscrim= "? Attr/colorprimary "property to work together more perfectly.
In this example, we also set the following:
app:layout_scrollFlags=scroll|exitUntilCollapsed>
To receive a:
app:[email protected]/appbar_scrolling_view_behavior>
This will result in a scrolling effect, and by Layout_collapsemode, we have set the effect of changing the content when scrolling.
Take another look at an official example:
Coordinatorlayout with Custom view
One thing to be aware of is that coordinatorlayout doesn't know how Floatingactionbutton or appbarlayout works internally- It simply provides an additional API in the form of Coordinator.behavior, which enables child view to better control touch events and gestures, and to declare dependencies between them, and receive callbacks through Ondependentviewchanged ().
You can use Coordinatorlayout.defaultbehavior (your View.Behavior.class) annotation or use app:layout_behavior= in the layout " Com.example.app. Your View$behavior property to define the default behavior of the view. The framework makes it possible for arbitrary view and coordinatorlayout to be combined.
Summarize
After a few days of research, Google's proposed Android Design support Library is not meant to give these very good controls, in fact, these controls on GitHub basically can find corresponding. It is designed to give the official design guidance and further refine the MD design idea. This is the most important feature of the Android Design support Library. Of course, in all fairness, the use of these controls is not very user-friendly, too many packages lead to the entire effect is not very customizable, but this is Google's first step, it will be more awesome.
Demo
Finally, a fusion of MD and Android Design Support library demo for everyone to study, I believe that combined with the article and code, you will be able to quickly understand the use of the Android Design support library.
Android Design Support Library use detailed