Reference article:
Geek College –material Design Chinese version of graphic tutorial
A few lines of code to make your APP become fancy-android Design support Library Code experiment
Material Design's Android open source collation
Material design app for low version Android
Android5.0 version, the introduction of the concept of material design, this is a major breakthrough in designing Android. The corresponding program implementation is like Theme.Material.Light, Theme.Material.Light.DarkActionBar theme, but this style theme can only be used in the 5.0 version of the phone, if 5.0 before the application of the Material design how to do?
Google official, and some Daniel, give us some programs to be compatible with the previous version of 5.0, commonly used packages are as follows:
‘com.android.support:appcompat-v7:22.+‘// 这个包是为了考虑API level 7(即Android 2.1)及以上版本而设计的,v7里默认包含了v4包,v7支持了Action Bar以及一些Theme的兼容。‘com.android.support:recyclerview-v7:22.+‘// 当你list中的元素经常动态改变时可以使用RecyclerView控件。它提供了如下两个功能:1、为每个条目位置提供了layout管理器(RecyclerView.setLayoutManager)2、为每个条目设置了操作动画(RecyclerView.setItemAnimator)‘com.android.support:cardview-v7:22.+‘// 卡片式视图组件,允许你在Card视图中显示信息, CardView也可以设置阴影和圆角。
Material Design's Theme
The topics of MD are:
@android: style/theme.material (Dark Version)
@android: Style/theme.material.light (Light version)
@android: Style/theme.material.light.darkactionbar
The corresponding Compat Theme in APPCOMPAT-V7:
Theme.appcompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar
We can customize color Palette (color palette) According to the style of our app, focusing on the following properties:
Value/styles.xml:
<resources> <stylename="Apptheme"Parent="Appbasetheme"/> <!--Base application theme.<stylename="Appbasetheme"Parent="Theme.AppCompat.Light.NoActionBar"> <!--Customize your theme here.<Item name="Colorprimary"> @color/colorprimary</Item> <Item name="Colorprimarydark"> @color/colorprimarydark</Item> <Item name="Coloraccent"> @color/coloraccent</Item> <Item name="Android:windowbackground"> @color/coloraccent</Item> </style> <stylename="Apptheme.appbaroverlay"Parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> <stylename="Apptheme.popupoverlay"Parent="ThemeOverlay.AppCompat.Light"/></resources>
which
1, coloraccent corresponding to EditText when editing, RadioButton selected, checkbox and other colors when selected.
2. Apptheme.appbaroverlay Specifies the style of the AppBar theme,
3, Apptheme.popupoverlay custom Toolbar pop-up menu style, such as "ThemeOverlay.AppCompat.Light", then this overflow pop up is white black word.
The corresponding graph of the attributes:
Metarial Design's Theme allows us to set the color of the status bar, if your project's minimum supported version is 5.0, then you can use Android:Theme.Material to set up Android: Statusbarcolor. Of course, this situation is relatively small, so most of us use Theme.appcompat, set the status bar color by setting the Android:colorprimarydark. (PS: By default, the value of Android:statusbarcolor is inherited from Android:colorprimarydark).
For devices under 5.0, current Colorprimarydark and Navagationbar may not be able to personalize the status color.
A simple demo:
In the Vaules/styles.xml file, define the following style:
<resources> <stylename="Apptheme"Parent="Appbasetheme"/> <!--Base application theme.<stylename="Appbasetheme"Parent="Theme.AppCompat.Light.NoActionBar"> <!--Customize your theme here.<Item name="Colorprimary"> @color/colorprimary</Item> <Item name="Colorprimarydark"> @color/colorprimarydark</Item> <Item name="Coloraccent"> @color/coloraccent</Item> <!--<item name= "Android:windowbackground" > @color/coloraccent</item> --</style> <stylename="Apptheme.appbaroverlay"Parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> <stylename="Apptheme.popupoverlay"Parent="ThemeOverlay.AppCompat.Light"/></resources>
Create the Styles.xml file under the Values-v21 folder:
<resources> <style name=" Apptheme " parent=" Appbasetheme " > //is true, the system will draw the status bar in your window, the default is true, the reason is to write because your theme may be inherited, ensure that it is true. <item name= "Android: Windowdrawssystembarbackgrounds " >true </ item > <item name=" Android:statusbarcolor " > @android: Color/transparent</item > <item name= "Android: Navigationbarcolor " > @color/colorprimary</item > </style> </resources>
Android4.2 Effect:
Android5.0 Effect:
Use of toolbar
As we all know, in the use of Actionbar, a bunch of questions: This text can be customized, position can change, the spacing of the icon to control God horse, thus exposing the actionbar design of the inflexible. For this purpose the official toolbar was provided and the Supprot library was provided for backwards compatibility. toolbar is flexible because it is actually a viewgroup, and we declare it in the layout file just like normal components when we use it.
(1) Introduction of toolbar
1) Hide the original Actionbar
Hidden can be modified by modifying the topics we inherit: Theme.AppCompat.Light.NoActionBar, such as Styles.xml:
<resources> <stylename="Apptheme"Parent="Appbasetheme"/> <!--Base application theme.<stylename="Appbasetheme"Parent="Theme.AppCompat.Light.NoActionBar"> <!--Customize your theme here.<Item name="Colorprimary"> @color/colorprimary</Item> <Item name="Colorprimarydark"> @color/colorprimarydark</Item> <Item name="Coloraccent"> @color/coloraccent</Item> <!--<item name= "Android:windowbackground" > @color/coloraccent</item> --</style> ...
Of course, you can also do this by setting the following properties:
<itemname="windowActionBar">false</item><itemname="android:windowNoTitle">true</item>
2) The layout file declares:
... <android.support.design.widget.appbarlayout android:id= "@+id/appBarLayout" Android:layout_height= "wrap_content" android:layout_width=" match_parent " android:theme=" @style/apptheme.appbaroverlay " > <android.support.v7.widget.toolbar android:id= "@+id/toolbar" A Ndroid:layout_width= "match_parent" android:layout_height=" attr/actionbarsize " android:background=" attr/colorprimary " App:popuptheme= "@style/apptheme.popupoverlay" /> </android.support.design . Widgets. appbarlayout> ...
You can see a layer of toolbar outside. Appbarlayout,appbarlayout is a control provided by the Android.support.design library, see the Android Design support library below for details.
Android Design Support Library
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:
‘com.android.support:design:22.+‘// 这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android 2.1 。 这些组件在我看来就是对 Github中最近比较火的 android 组件进行了封装。
Snackbar Lightweight pop-up window
Snackbar provides a lightweight control between toast and Alertdialog, which makes it easy to provide prompt and action feedback for messages.
Snackbar. make(View,"Snackbar comes Out", Snackbar. LENGTH_long). Setaction("Action", New View. Onclicklistener() {@Override public void OnClick (View v) { Toast. Maketext(hymainactivity. this,"Toast comes Out", Toast. LENGTH_short). Show();} } ). Show();
Textinputlayout Enhanced Text input
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.
... <android.support.design.widget.TextInputLayout android:id="@+id/til_pwd" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout> ...
...FinalTextinputlayout textinputlayout = (textinputlayout) getactivity (). Findviewbyid (R.ID.TIL_PWD); EditText EditText = Textinputlayout.getedittext (); Textinputlayout.sethint ("Please enter your password"); Edittext.addtextchangedlistener (NewTextwatcher () {@Override Public void beforetextchanged(Charsequence S,intStartintCountintAfter) {if(S.length () >Ten|| S.length () <6) {Textinputlayout.seterror ("Please enter 6 10-digit password"); Textinputlayout.seterrorenabled (true); }Else{textinputlayout.seterrorenabled (false); } }@Override Public void ontextchanged(Charsequence S,intStartintBefore,intCount) {Textview.settext (s); }@Override Public void aftertextchanged(Editable s) { } }); ...
It is important to note that the color of the textinputlayout comes from the color of the coloraccent in the style.
Floating Action Button interface operation buttons
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 hover action button with a default color of coloraccent in the theme. Floatingactionbutton inherits from ImageView, you can use any method of android:src or ImageView, such as setimagedrawable () To set the icon inside the Floatingactionbutton.
<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@drawable/shape_bg"/>
Tablayout Slide to toggle layout
The tablayout of the Design library implements both a fixed tab-average width distribution of the view and a scrollable tab-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"...
But most of the time we will not use this, usually the sliding layout will be used in conjunction with the Viewpager, only the use of Tablayout.setupwithviewpager (Mviewpager), the two is linked together.
Navigationview Navigation Drawer
Google provides navigationview to implement the navigation menu interface, the most important of which is these two properties:
App:headerlayout
App:menu
With these two properties, we can easily specify the head layout and menu layout of the Navigation interface:
<?xml version= "1.0" encoding= "Utf-8"?><android.support.v4.widget.drawerlayout 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:id =" @+id /drawer_layout " android:layout_width =" match _parent " android:layout_height =" Match_ Parent " android:fitssystemwindows =" true " tools:opendrawer = "start" ; <include layout =< Span class= "Hljs-value" > "@layout/hy_appbar_main" android:layout_width = "match_parent" android:layout_height = "match_parent" /> <android.support.design.widget.navigationview android:id = "@+id/nav_view" android:layout_width =" wrap_content " android:layout_height =" match_parent " android:layout_gravity =" start " android:fitssystemwindows =" true " app:headerlayout =" @layout/hy_nav_header_main " app:menu = "@menu/hy_main_drawer" /> </android.support.v4.widget.DrawerLayout>
Android5.0 New Features Learning summary