欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié)

 更新時(shí)間:2016年04月07日 15:57:45   作者:匆忙擁擠repeat  
這篇文章主要介紹了Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié),文中還給了一個(gè)RecyclerView布局管理的例子,需要的朋友可以參考下

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)文章

最新評(píng)論