Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié)
Define Alternative Styles 定義替代樣式
讓你的app,使用Material Design的主題運(yùn)行在支持它的設(shè)備上,并在早期版本的設(shè)備上可以運(yùn)行較早的主題:
1. 在res/values/styles.xml 定義一個(gè)主題繼承較早的主題
2. 在res/values-v21/styles.xml 定義一個(gè)相同名字的繼承自Material主題 的主題
3. 在manifest中應(yīng)用定義的主題
注:如果你的app使用了Material 主題,而不提供較早的主題,那么將不能運(yùn)行在早期版本的設(shè)備上
Provide Alternative Layouts 提供替代布局
如果你設(shè)計(jì)的layout不引用任何的5.0中的xml屬性,那么可以運(yùn)行在早期版本的Android設(shè)備上。否則,你可提供一個(gè)替代布局。
替代布局建立在res/layout-v21/
為了避免重復(fù)代碼,可以在res/values/ 定義你的styles,新風(fēng)格的在res/values-21/ 中定義,并使用style的繼承,在res/values中定義一個(gè)baseStyle,在res/values-21中繼承它。
Use the Support Library 使用支持庫
v7 support library 包括以下的一些特性:
- 在應(yīng)用了一個(gè)Theme.AppCompat 主題后,系統(tǒng)的一些組件就有了Material Design 的風(fēng)格
- 在Theme.AppCompat 主題中,有調(diào)色主題
- RecyclerView 組件顯示數(shù)據(jù)集
- CardView 組件創(chuàng)建卡片
- 從圖像中取色
System widgets 系統(tǒng)組件
Theme.AppCompat 主題提供的Material Design 風(fēng)格的組件有:
- EditText
- Spinner
- CheckBox
- Radiobutton
- SwitchCompat
- CheckedTextView
Color Palette
使用v7支持庫,獲得Material Design 風(fēng)格定義顏色板,應(yīng)用一個(gè)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版本上也可運(yùn)行。
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 檢查系統(tǒng)版本
以下特性只能在Android 5.0(API級(jí)別21)及以上:
- Activity transitions 活動(dòng)轉(zhuǎn)換
- Touch feedback 觸覺反饋
- Reveal animations 顯示動(dòng)畫
- Path-based animations 基于路徑動(dòng)畫
- Vector drawables 矢量圖片
- Drawable tinting 圖片染色
檢查代碼:
// 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 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提供這些內(nèi)置的布局管理器: * linearlayoutmanager 顯示垂直滾動(dòng)列表或水平的項(xiàng)目。 * gridlayoutmanager 顯示在一個(gè)網(wǎng)格項(xiàng)目。 * staggeredgridlayoutmanager 顯示在交錯(cuò)網(wǎng)格項(xiàng)目。 * 自定義的布局管理器,需要繼承recyclerview.layoutmanager類。 * * add/remove items時(shí)的動(dòng)畫是默認(rèn)啟用的。 * 自定義這些動(dòng)畫需要繼承RecyclerView.ItemAnimator,并實(shí)現(xiàn)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內(nèi)容反轉(zhuǎn) mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false); //HORIZONTAL 橫向滾動(dòng)顯示內(nèi)容 VERTICAL縱向 // mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.HORIZONTAL, false); //方向也是指示滾動(dòng)方向,例子中橫向開頭的數(shù)據(jù)交錯(cuò)了一點(diǎn), 縱向的無交錯(cuò) // mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL); // mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); // mRecyclerView.setLayoutManager(new MyLayoutMnager()); //數(shù)據(jù)不顯示,可能還需要重寫什么東西。。 // 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); //構(gòu)建一個(gè)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; } } }
相關(guān)文章
Flutter onTap中讓你脫穎而出的5條規(guī)則
這篇文章主要為大家介紹了Flutter onTap中讓你脫穎而出的5條規(guī)則,小事情決定了你的熟練程度,這些小細(xì)節(jié)的有趣之處在于它們的豐富性2023-11-11完美解決客戶端webview持有的頁面緩存,不會(huì)立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會(huì)立即釋放的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12詳解android特性之CoordinatorLayout用法探析實(shí)例
本篇文章主要介紹了android特性之CoordinatorLayout用法探析實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Android studio實(shí)現(xiàn)加法軟件
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)加法軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android下拉阻尼效果實(shí)現(xiàn)原理及簡單實(shí)例
這篇文章主要為大家詳細(xì)介紹了Android下拉阻尼效果實(shí)現(xiàn)原理及簡單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android性能測試關(guān)注的指標(biāo)整理
在本篇文章里小編給各位整理的是關(guān)于Android性能測試關(guān)注的指標(biāo)整理內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-10-10Android 手機(jī)瀏覽器調(diào)試使用Chrome進(jìn)行調(diào)試實(shí)例詳解
這篇文章主要介紹了Android 手機(jī)瀏覽器調(diào)試使用Chrome進(jìn)行調(diào)試實(shí)例詳解的相關(guān)資料,這里提供了實(shí)例,需要的朋友可以參考下2016-12-12