Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 5.X 全新特性巡禮:Material Design 常用組件實戰

編輯:關於 Android 程式設計

要在項目中使用 Android 5.X 的新特性,第一步是導入 5.X 的支持包。

FloatingActionButton

這是 Material Design 設計規範中那種帶陰影效果的懸浮按鈕。使用前需先引入 design 包。FloatingActionButton 是 ImageView 的子類。

先看它在布局中的屬性設置,需在根布局下添加:

xmlns:app="https://schemas.android.com/apk/res-auto"

相關屬性說明:

app:backgroundTint——當前按鈕顯示的顏色。

app:rippleColor——波紋顏色。

app:borderWidth——邊緣寬度。

app:elevation——按鈕的懸浮海拔,決定陰影大小,海拔越大陰影越大。

app:pressedTranslationZ——按下時按鈕在 Z 軸的位移,決定陰影動畫,Z 軸越大陰影越大。

代碼中的實現:

floatingActionButton= (FloatingActionButton) findViewById(R.id.fab); floatingActionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v,"this is a sncakbar",Snackbar.LENGTH_LONG).setAction("Action", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "我被點擊了", Toast.LENGTH_SHORT).show(); } }).show(); } });

CoordinatorLayout

CoordinatorLayout 是一種強大的 FrameLayout,主要用於協調各個子布局之間的聯動,尤其適合子控件間存在關聯動畫的場景,此時要求子控件具備相應的 behavior。

它的主要功能有三:作為根布局使用、協調子布局、添加錨點。

在 xml 中把根布局設為 CoordinatorLayout,並在布局中聲明:

xmlns:app="https://schemas.android.com/apk/res-auto"

在子布局中可使用:

設置子布局的錨點(默認為父控件):app:layout_anchor="@id/appbar"

設置布局(一般用支持包提供的控件)在錨點控件上的位置:app:layout_anchorGravity="bottom|start"

AppBarLayout 與 Toolbar

AppBarLayout

AppBarLayout 是繼承自垂直方向 LinearLayout 的 ViewGroup 容器組件,專為 Material Design 的 App Bar 而生,支持手勢滑動操作。它的作用是把被它包裹的內容都當作 App Bar 來處理。

Toolbar

Toolbar 是工具欄。由於 Android 早期的標題欄設計使用起來頗為不便,Google 推出了向下兼容的工具欄。若要把 Toolbar 當作標題欄,必須調用 setSupportActionBar(),否則它不會顯示。Toolbar 提供了更豐富的功能。

toolbar= (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //設置toolbar的左邊的返回或者是home的按鈕 final ActionBar ab = getSupportActionBar(); ab.setHomeAsUpIndicator(R.mipmap.ic_done); ab.setDisplayHomeAsUpEnabled(true); //設置toolbar的左邊的返回或者是home的按鈕的點擊事件 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "點擊了返回按鈕", Toast.LENGTH_SHORT).show(); } });

我們還可以在 Toolbar 中添加一些簡單的點擊按鍵。

這裡和 ActionBar 一樣,需要自己布局 menu 菜單項:

          

接著在代碼中為 toolbar 創建菜單:

//創建toolbar的按鈕菜單 public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.simple_action,menu); return true; } //用來響應側邊三個點的那個菜單項當前的選中項 public boolean onPrepareOptionsMenu(Menu menu) { switch (AppCompatDelegate.getDefaultNightMode()) { case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM: menu.findItem(R.id.menu_night_mode_system).setChecked(true); break; case AppCompatDelegate.MODE_NIGHT_AUTO: menu.findItem(R.id.menu_night_mode_auto).setChecked(true); break; case AppCompatDelegate.MODE_NIGHT_YES: menu.findItem(R.id.menu_night_mode_night).setChecked(true); break; case AppCompatDelegate.MODE_NIGHT_NO: menu.findItem(R.id.menu_night_mode_day).setChecked(true); break; } return true; } //設置相應的toolBar子控件的點擊事件 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: //在這裡同樣可以添加toolbar的home鍵的點擊事件 return true; case R.id.menu_night_mode_system: setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); break; case R.id.menu_night_mode_day: setNightMode(AppCompatDelegate.MODE_NIGHT_NO); break; case R.id.menu_night_mode_night: setNightMode(AppCompatDelegate.MODE_NIGHT_YES); break; case R.id.menu_night_mode_auto: setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); break; } return super.onOptionsItemSelected(item); } //設置相應的夜間/白天模式 private void setNightMode( int nightMode) { AppCompatDelegate.setDefaultNightMode(nightMode); if (Build.VERSION.SDK_INT >= 11) { recreate(); } }

DrawerLayout 與 NavigationView

二者結合使用,即可實現簡潔的側滑菜單效果。

DrawerLayout

DrawerLayout 是 ViewGroup 的子類,作為界面的頂層容器,允許抽屜窗口從左右邊緣拖出。抽屜的位置由 layout_gravity 控制,start 代表右邊、end 代表左邊(在國內的 Layout 方向下)。抽屜窗口的高度一般設為 match_parent。DrawerLayout.DrawerListener 可用於監測抽屜視圖的狀態與動畫,注意避免在其中執行昂貴(消耗較多內存與 CPU)的操作,例如自定義動畫。

NavigationView

NavigationView 是 FrameLayout 的子類,主要用於添加應用的常用菜單欄,通常配合 DrawerLayout 使用。它主要包含兩個屬性:headerLayout(界面的頭布局)與 menu(界面的菜單欄)。上方 menu 的點擊事件在 onNavigationItemSelected 監聽器中實現。

先來看側滑菜單的布局代碼。這裡採用主布局引用(include)的方式,讓結構更清晰(注意:這行說明性注釋需刪除,不能這樣注釋):

layout_head.xml:

  

sliding_menu.xml(位於 menu 文件夾下):

             

主布局 layout_main.xml 中放入 DrawerLayout、NavigationView 等核心容器。接下來是邏輯代碼的實現:

//控件的實例化 drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (navigationView != null) { setupDrawerContent(navigationView); }

為側滑菜單的菜單項設置點擊事件:

//設置側滑菜單的點擊事件 private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(true); drawerLayout.closeDrawers(); return true; } }); }

點擊 toolbar 的 home 鍵時,這樣處理邏輯:

case android.R.id.home: drawerLayout.openDrawer(GravityCompat.START); return true;

CollapsingToolbarLayout

CollapsingToolbarLayout 提供了一個可折疊的 Toolbar,它繼承自 FrameLayout。設置 layout_scrollFlags,可以控制其中所包含的控件(如 ImageView、Toolbar)在響應 layout_behavior 事件時的 scrollFlags 行為(移出屏幕或固定在頂端)。設置 contentScrim 可控制滾動前後的背景色;在子控件中設置 layout_collapseMode 則能更改折疊的動畫效果。

CollapsingToolbarLayout 提供了可折疊的標題欄。如果滑動事件並非發生在標題欄上(例如發生在 ScrollView 中),就需要改用 NestedScrollView,並為它指定相應的 behavior,這樣 CoordinatorLayout 才能協調它們的滑動事件。

簡單 Demo

最後附上一個簡單的 Demo 供大家共同學習:傳送門——Android 5.X 新特性簡單 Demo(GitHub)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved