Define alternative Styles define alternative styles
let your app run on a device that supports it using the material design theme, and you can run earlier topics on earlier versions of the device:
1. Define a theme in Res/values/styles.xml to inherit an earlier topic
2. In Res/values-v21/styles.xml, define an inherited theme of the same name from material theme
3. Apply a defined theme in manifest
Note: If your app uses a material theme and does not offer an older theme, it will not be able to run on earlier versions of the device
Provide alternative Layouts provides alternative layouts
If you design a layout that does not reference any of the 5.0 XML attributes, you can run it on an earlier version of the Android device. Otherwise, you can provide an alternative layout.
Alternate layout based on res/layout-v21/
To avoid duplicate code, you can define your styles, new style in res/values-21/, and use the inheritance of style to define a basestyle in Res/values, in the res/of res/values/. Inherit it from the values-21.
Use the Support library with support libraries
The V7 Support library includes some of the following features:
- After applying a theme.appcompat theme, some components of the system have a material design style
- In the Theme.appcompat theme, there is a palette theme
- Recyclerview Component Display DataSet
- CardView Component Creation Card
- Take color from the image
System Widgets Systems Components
The material design-style components provided by the Theme.appcompat theme are:
- EditText
- Spinner
- CheckBox
- Radiobutton
- Switchcompat
- Checkedtextview
Color Palette
Using the V7 Support library, get the material design style definition color board and apply a Theme.appcompat theme:
<!--extend one of the Theme.appcompat themes--> <style name= "Theme.mytheme"
Theme.AppCompat.Light ">
<!--Customize the color palette-->
<item name=" Colorprimary "> @color /material_blue_500</item>
<item name= "Colorprimarydark" > @color/material_blue_700</item>
<item name= "coloraccent" > @color/material_green_a200</item>
</style>
Lists and Cards
After using the V7 support library, it can also be run on earlier versions of Android.
Dependencies
Gradle Dependence:
dependencies {
compile ' com.android.support:appcompat-v7:21.0.+ '
compile ' com.android.support:cardview-v7 : 21.0.+ '
compile ' com.android.support:recyclerview-v7:21.0.+ '
}
Check the system version
The following features are available only at the Android 5.0 (API level 21) and above:
- Activity Transitions Active Conversion
- Touch Feedback Tactile Feedback
- Reveal Animations Display Animation
- path-based animations based on path animation
- Vector drawables Image
- drawable Tinting Picture Coloring
Check code:
Check if we ' re running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= build.version_codes. Lollipop) {
//Call some material design APIs here
} else {
//implement this feature without material desi GN
}
Note: For app to support 5.0, you need to android:targetsdkversion=21 in manifest.
Ps:recyclerview
examples attached to Recyclerview:
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v7.widget.GridLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.support.v7.widget.RecyclerView.LayoutParams;
Import Android.view.LayoutInflater;
Import Android.view.ViewGroup;
Import Android.widget.TextView; The public class Recyclerviewactivity extends activity {/* * Recyclerview provides these built-in layout managers: * Linearlayoutmanager
Displays a vertical scrolling list or horizontal item.
* Gridlayoutmanager display in a grid item.
* Staggeredgridlayoutmanager display in staggered grid items.
* Custom layout Manager, you need to inherit the Recyclerview.layoutmanager class.
* * Add/remove items when the animation is enabled by default.
* Custom These animations need to inherit recyclerview.itemanimator, and realize recyclerview.setitemanimator () * * Private Recyclerview Mrecyclerview;
Private Recyclerview.adapter Madapter;
Private Recyclerview.layoutmanager Mlayoutmanager;
Private string[] myDataSet; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancesTate);
Setcontentview (R.layout.recycler_view);
Mrecyclerview = (Recyclerview) Findviewbyid (R.id.my_recycler_view); Use this setting to improve performance if your know that changes//in content did not change the layout size of th
E Recyclerview mrecyclerview.sethasfixedsize (true);
Use a linear layout manager//Mlayoutmanager = new Linearlayoutmanager (this);
Mlayoutmanager = new Gridlayoutmanager (this, 3, gridlayoutmanager.vertical, true);
True to reverse the layout content Mlayoutmanager = new Gridlayoutmanager (this, 3, gridlayoutmanager.vertical, false); Horizontal horizontal scrolling Display content Vertical Portrait//Mlayoutmanager = new Gridlayoutmanager (this, 3, Gridlayoutmanager.horizontal, FAL
SE); The direction is also indicative of the scrolling direction, in which the horizontal opening of the data in the example is staggered, with the longitudinal no interleaving//Mlayoutmanager = new Staggeredgridlayoutmanager (3, Staggeredgridlayoutmanager .
horizontal);
Mlayoutmanager = new Staggeredgridlayoutmanager (4, staggeredgridlayoutmanager.vertical);
Mrecyclerview.setlayoutmanager (Mlayoutmanager); Mrecyclerview.setlayoutmanager (New Mylayoutmnager ());
Data is not displayed, you may need to rewrite something ...
Specify an adapter (also next example) Setdatas ();
Madapter = new Myadapter (myDataSet);
Mrecyclerview.setadapter (Madapter);
private void Setdatas () {int len = 200;
myDataSet = new String[len];
for (int i = 0; i < len, i++) {switch (i%3) {case 0:mydataset[i] = "China" + i;
Break
Case 1:mydataset[i] = "United States" + I;
Break
Case 2:mydataset[i] = "Australia" + i;
Break }} class Mylayoutmnager extends Recyclerview.layoutmanager {@Override public layoutparams g Eneratedefaultlayoutparams () {layoutparams params = new Layoutparams (layoutparams.wrap_content, LayoutParams.WRAP_
CONTENT);
Params.topmargin = 5;
return params; } class Myadapter Extends recyclerview.adapter<viewholder> {private string[] mdataset; Provide a reference to the view for each data item//Complex data items may need more than a d//You are provide access to all of the views for a data item in a view holder//provide a suitable constructor (d
Epends on the kind of datasets) public Myadapter (string[] mydataset) {mdataset = myDataSet; }//Create new View (invoked by the layout manager) @Override public Viewholder Oncreateviewholder Group parent, int viewtype) {//Create a new view TextView TV = (TextView) layoutinflater.from (Parent.getco
ntext ()). Inflate (R.layout.my_text_view, parent, false);
Set the view ' s size, margins, paddings and layout parameters//... Viewholder VH = new Viewholder (TV);
Build a viewholder return VH; }//Replace the contents of a view (invoked by the layout manager) @OverrIDE public void Onbindviewholder (viewholder holder, int position) {//-get element from your dataset at this Position//-Replace the contents of the view with that element Holder.mTextView.setText (mdataset[position
]); //Return to the size of your dataset (invoked by the layout manager) @Override public int GetItemCount ()
{return mdataset.length; The static class Viewholder extends Recyclerview.viewholder {//Each data item was just a string in this CA
SE public TextView mtextview;
Public Viewholder (TextView v) {super (V);
Mtextview = v;
}
}
}