Define Alternative Styles Define alternative styles
Let your app run on devices that support it using Material Design themes and run older themes on earlier versions of devices:
1. Define a topic in res/values/ to inherit an earlier topic
2. Define a theme with the same name inherited from the Material theme in res/values-v21/
3. Apply defined topics in manifest
Note: If your app uses Material themes and does not provide earlier themes, it will not run on earlier versions of the device.
Provide Alternative Layouts Provide Alternative Layouts
If the layout you designed does not reference any xml properties in 5.0, it can run on earlier versions of Android devices. Otherwise, you can provide an alternative layout.
Alternative layout is built on res/layout-v21/
To avoid duplicate code, you can define your styles in res/values/ , define your style in res/values-21/, and use style inheritance, define a baseStyle in res/values, and inherit it in res/values-21.
Use the Support Library
v7 support library includes some of the following features:
- After applying a theme, some components of the system have the style of Material Design
- Among the themes, there are color tuning themes
- RecyclerView component displays dataset
- CardView component creates cards
- Get color from the image
System widgets System components
The Material Design-style components provided by the theme are:
- EditText
- Spinner
- CheckBox
- Radiobutton
- SwitchCompat
- CheckedTextView
Color Palette
Use the v7 support library to get the Material Design style definition color board, and apply a theme:
<!-- extend one of the themes --> <style name="" parent=""> <!-- 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 run on earlier Android versions.
Dependencies
gradle dependencies:
dependencies { compile ':appcompat-v7:21.0.+' compile ':cardview-v7:21.0.+' compile ':recyclerview-v7:21.0.+' }
Check the System Version Check the system version
The following features can only be found in Android 5.0 (API level 21) or above:
- Activity transitions Activity transitions
- Touch feedback Touch feedback
- Reveal animations Display animations
- Path-based animations
- Vector drawables Vector image
- Drawable tinting Picture dyeing
Check the code:
// Check if we're running on Android 5.0 or higher if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Call some material design APIs here } else { // Implement this feature without material design }
Note: To make the app support 5.0, you need to Android: targetSdkVersion=21 in manifest.
PS:RecyclerView
Attached with a RecyclerView example:
import ; import ; import .; import .; import .; import ; import ; import ; public class RecyclerViewActivity extends Activity { /* * Recyclerview provides these built-in layout managers: * linearlayoutmanager Displays vertical scrolling list or horizontal items. * gridlayoutmanager is displayed in a grid item. * staggeredgridlayoutmanager displays in the interleaved grid item. * Custom layout manager, need to inherit classes. * * Animation when add/remove items is enabled by default. * Customizing these animations requires inheritance and implementing () */ private RecyclerView mRecyclerView; private mAdapter; private mLayoutManager; private String[] myDataset; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.recycler_view); mRecyclerView = (RecyclerView) findViewById(.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView (true); // use a linear layout manager // mLayoutManager = new LinearLayoutManager(this); // mLayoutManager = new GridLayoutManager(this, 3, , true); //true means that the layout content is reversed mLayoutManager = new GridLayoutManager(this, 3, , false); //HORIZONTAL Scroll horizontally display content VERTICAL Portrait// mLayoutManager = new GridLayoutManager(this, 3, , false); //The direction also indicates the scrolling direction. In the example, the data at the beginning of the horizontal direction is interleaved a little, and the vertical direction is not interleaved.// mLayoutManager = new StaggeredGridLayoutManager(3, ); // mLayoutManager = new StaggeredGridLayoutManager(4, ); (mLayoutManager); // (new MyLayoutMnager()); // The data is not displayed, and something may need to be rewrited. . // specify an adapter (see also next example) setDatas(); mAdapter = new MyAdapter(myDataset); (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] = "USA" + i; break; case 2: myDataset[i] = "Australia" + i; break; } } } class MyLayoutMnager extends { @Override public LayoutParams generateDefaultLayoutParams() { LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); = 5; return params; } } class MyAdapter extends <ViewHolder> { private String[] mDataset; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder // Provide a suitable constructor (depends on the kind of dataset) public MyAdapter(String[] myDataset) { mDataset = myDataset; } // Create new views (invoked by the layout manager) @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view TextView tv = (TextView) (()) .inflate(.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 (mDataset[position]); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return ; } } static class ViewHolder extends { // each data item is just a string in this case public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } }