1. 程式人生 > >從零開始開發一款Android App

從零開始開發一款Android App

Google在I/O 2014上推出Material Design(材料設計)規範後,被外界稱讚:“Google 第一次在設計方面超越了Apple。”,隨著Android5.0的市場佔有率增加,Material Design越來越被開發者和使用者所接受。本文內容旨在介紹Material Design在具體開發中的使用;如果有對Material Design不瞭解的朋友,請先參考官網介紹

“Material Design”推出很多的新控制元件,如RecyclerView、CardView、Drawable、FloatingActionButton等等。本篇不討論這些控制元件的使用方法,簡書的“Android材料設計”

專題裡面的大神們已經寫了很多,如果有控制元件使用方面的疑慮,可以參考該專題;本篇將討論一些實際性的問題:

玩轉“Material Design”

先教大家一個小技巧:如果專案中想要使用“Material Design”相關東西,新建專案時在"Add an Activity to Mobile"這個選擇框時選擇"Navigation Drawer Activity",而不是選擇“Empty Activity”,這時,新建的專案就會有材料設計的框架。在這個基礎上開發,會避免一些麻煩。否則,如果對材料設計的各種style和各類包不熟悉的話,在編譯的時候會出很多問題;


Navigation_Drawer.png

新建工程的build.gradle檔案中,看到AS已經自動匯入了你當前SDK最新的“材料設計”的v7包:

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'

執行新建的工程檔案,一個帶側滑和沉浸式狀態列的的空專案完成。此時我們就來研究一下如何實現沉浸式狀態列:

沉浸式狀態列

先看看效果圖:


沉浸式狀態列.png

沉浸式狀態列:即 將狀態列的顏色改成ToolBar顏色的深色版本。使得狀態列也成為App的一部分,而不是像5.0以下版本那樣一成不變的灰色或黑色。

方法一:
在AndroidManifest.xml可以看到Application使用的Theme是自定義的style “AppTheme”。於是,我們開啟在values資料夾下看到AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Customize your theme here. -->    
  <item name="colorPrimary">@color/colorPrimary</item>    
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>     
</style>

這裡的colorParimary是Toolbar的顏色,而colorParimaryDark是狀態列的顏色,兩個顏色請自行設定;

方法二
同時我們注意到:MainActivity由於需要新增NavigationView,所以要自己寫ToolBar。因此這裡的Theme可以自定義AppTheme.NoActionBar(寫好的Toolbar後會預設使用AppTheme的沉浸式狀態列的配色)。但由於是自定義主題,並不是V7包的內容,則需要在values和values-v21兩個資料夾下都寫一個同名的style以適配5.0以下版本的機型:
values下的AppTheme.NoActionBar:

<style name="AppTheme.NoActionBar">    
  <item name="windowActionBar">false</item>    
  <item name="windowNoTitle">true</item>
</style>

values-v21下的AppTheme.NoActionBar:

<style name="AppTheme.NoActionBar">    
  <item name="windowActionBar">false</item>    
  <item name="windowNoTitle">true</item>    
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>    
  <item name="android:statusBarColor">@android:color/transparent</item>
</style>

此時兩種設定沉浸式狀態列的方法完成,如果你對沉浸式狀態列的配色不滿意,請將這個網站推薦給你的設計師;

ToolBar跟隨滑動至消失

效果圖:


Toolbar跟隨滑動.gif

Toolbar跟隨滑動至消失這個效果是一個很容易但是互動性比較強的小動畫;

<android.support.design.widget.AppBarLayout    
  android:layout_width="match_parent"    
  android:layout_height="wrap_content"    
  android:fitsSystemWindows="true"            
  android:theme="@style/AppTheme.AppBarOverlay"> 

   <android.support.v7.widget.Toolbar        
      android:id="@+id/toolbar"        
      android:layout_width="match_parent"        
      android:layout_height="?attr/actionBarSize"        
      app:layout_scrollFlags="scroll|enterAlways|snap"        
      app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout       
      android:id="@+id/tab_top"    
      android:layout_width="match_parent"    
      android:layout_height="wrap_content">    
      </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

從這裡看到,我們寫Toolbar的時候需要巢狀在AppBarLayout裡面,AppBarLayout的好處就是可以讓Toolbar配合TabLayout一同使用;要實現Toolbar跟隨滑動,只需要在AppBarLayout中新增:

android:fitsSystemWindows="true"

在ToolBar中新增:

app:layout_scrollFlags="scroll|enterAlways|snap"

但是要注意的是:AppBarLayout應該配合CoordinatorLayout使用,同時,ToolBar下的主體部分必須是,RecyclerView、NestedScrollView等實現NestedScrollView介面的可滑動部件;ListView可以在外面巢狀一層NestedScrollView來觸發CoordingatorLayout的滑動;佈局程式碼

本篇結束語

本篇僅僅寫了“Material Design”的九牛一毛,但是我認為這是上面的兩個東西是專案中最常用的,希望我的文章已經解釋清楚了。接下來,我的Demo中還會陸續寫到RecyclerView、Drawer等控制元件的使用,雖然不會再另寫一篇部落格,但是還是希望大家可以關注我的系列文章:從零開始開發一款Android App

剛開始接觸“材料設計”這個概念的時候感覺很難,但是在用過一些控制元件之後,感覺材料設計的人機互動性很強,希望各位能夠真正將“材料設計”當成一種工具來實現。