Android應用的Material設計的布局相容性的一些要點總結_Android

來源:互聯網
上載者:User

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 包括以下的一些特性:

  • 在應用了一個Theme.AppCompat 主題後,系統的一些組件就有了Material Design 的風格
  • 在Theme.AppCompat 主題中,有調色主題
  • RecyclerView 組件顯示資料集
  • CardView 組件建立卡片
  • 從映像中取色

System widgets  系統組件

Theme.AppCompat 主題提供的Material Design 風格的組件有:

  • EditText
  • Spinner
  • CheckBox
  • Radiobutton
  • SwitchCompat
  • CheckedTextView

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)及以上:

  • Activity transitions  活動轉換
  • Touch feedback    觸覺反饋
  • Reveal animations  顯示動畫
  • Path-based animations  基於路徑動畫
  • Vector drawables  向量圖片
  • Drawable tinting  圖片染色

檢查代碼:

// 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;     }   } } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.