Define Alternative Styles 定义替代样式
让你的app,使用Material Design的主题运行在支持它的设备上,并在早期版本的设备上可以运行较早的主题:
1. 在res/values/styles.xml 定义一个主题继承较早的主题
2. 在res/values-v21/styles.xml 定义一个相同名字的继承自Material主题 的主题
3. 在manifest中应用定义的主题
注:如果你的app使用了Material 主题,而不提供较早的主题,那么将不能运行在早期版本的设备上
Provide Alternative Layouts 提供替代布局
如果你设计的layout不引用任何的5.0中的xml属性,那么可以运行在早期版本的Android设备上。否则,你可提供一个替代布局。
替代布局建立在res/layout-v21/
为了避免重复代码,可以在res/values/ 定义你的styles,新风格的在res/values-21/ 中定义,并使用style的继承,在res/values中定义一个baseStyle,在res/values-21中继承它。
Use the Support Library 使用支持库
v7 support library 包括以下的一些特性:
System widgets 系统组件
Theme.AppCompat 主题提供的Material Design 风格的组件有:
Color Palette
使用v7支持库,获得Material Design 风格定义颜色板,应用一个Theme.AppCompat 主题:
<!-- extend one of the Theme.AppCompat themes --><style name="Theme.MyTheme" parent="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
使用v7支持库后,在早期的Android版本上也可运行。
Dependencies
gradle 依赖:
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 检查系统版本
以下特性只能在Android 5.0(API级别21)及以上:
检查代码:
// Check if we're running on Android 5.0 or higherif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Call some material design APIs here} else { // Implement this feature without material design}
注:要让app支持5.0,需要在manifest中Android:targetSdkVersion=21。
PS:RecyclerView
附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; public class RecyclerViewActivity extends Activity { /* * recyclerview提供这些内置的布局管理器: * linearlayoutmanager 显示垂直滚动列表或水平的项目。 * gridlayoutmanager 显示在一个网格项目。 * staggeredgridlayoutmanager 显示在交错网格项目。 * 自定义的布局管理器,需要继承recyclerview.layoutmanager类。 * * add/remove items时的动画是默认启用的。 * 自定义这些动画需要继承RecyclerView.ItemAnimator,并实现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 you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager // mLayoutManager = new LinearLayoutManager(this); // mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, true); //true 表示,将layout内容反转 mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false); //HORIZONTAL 横向滚动显示内容 VERTICAL纵向 // mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.HORIZONTAL, false); //方向也是指示滚动方向,例子中横向开头的数据交错了一点, 纵向的无交错 // mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL); // mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); // mRecyclerView.setLayoutManager(new MyLayoutMnager()); //数据不显示,可能还需要重写什么东西。。 // specify an adapter (see 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] = "中国" + i; break; case 1: myDataset[i] = "美国" + i; break; case 2: myDataset[i] = "澳大利亚" + i; break; } } } class MyLayoutMnager extends RecyclerView.LayoutManager { @Override public LayoutParams generateDefaultLayoutParams() { 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 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) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); // set the view's size, margins, paddings and layout parameters //... ViewHolder vh = new ViewHolder(tv); //构建一个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 the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.length; } } static class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } }
新闻热点
疑难解答