編輯:關於 Android 程式設計
想使用 Material Design(MD)風格控件,第一步得先在 Gradle 中引入 Support Design Library,例如:
compile 'com.android.support:design:24.1.1'
一、CoordinatorLayout
1、CoordinatorLayout + AppBarLayout
版面配置程式碼如下:
<android.support.design.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" app:title="MdView" /> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> ................ </android.support.design.widget.CoordinatorLayout>
來看實際效果:

CoordinatorLayout_1
搭配 CoordinatorLayout 與 AppBarLayout,我們實現了這樣的效果:向上滾動列表時 Toolbar 會隱藏、TabLayout 貼齊頂端;再向下滾動時 Toolbar 又會重新顯示。
Toolbar 之所以能隨滾動隱藏或顯示,關鍵在於這個屬性:app:layout_scrollFlags="scroll|enterAlways"
相關屬性值說明如下:
scroll:需要隨滾動移出畫面的 view 必須設定這個 flag,否則該 view 會被固定在畫面頂端。
enterAlways:加上這個 flag 後,向下滾動會讓 view 恢復為可見狀態。
替 Toolbar 設定 layout_scrollFlags 只是第一步,還得在 CoordinatorLayout 中放一個可滾動的 View,這裡我們用 ViewPager,並在其中嵌套 RecyclerView。同時要為 ViewPager 加上這個屬性:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
這裡順帶說明 TabLayout 的用法:
//設置TabLayout可滾動,保證Tab數量過多時也可正常顯示 mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); //兩個參數分別對應Tab未選中的文字顏色和選中的文字顏色 mTabLayout.setTabTextColors(Color.WHITE, Color.RED); //綁定ViewPager mTabLayout.setupWithViewPager(mViewPager); //設置TabLayout的布局方式(GRAVITY_FILL、GRAVITY_CENTER) mTabLayout.setTabMode(TabLayout.MODE_FIXED); mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL); //設置TabLayout的選擇監聽 mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { //點擊Tab時回調 @Override public void onTabSelected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } //重復點擊Tab時回調 @Override public void onTabReselected(TabLayout.Tab tab) { scrollToTop(mFragments.get(tab.getPosition()).getTypeList()); } });2、CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout
版面配置程式碼如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:id="@+id/detail_content" tools:context="com.othershe.mdview.DetailActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="200dp" android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/detail_img" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="fitXY" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.7" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.NestedScrollView> ..................... </android.support.design.widget.CoordinatorLayout>
來看效果:

CoordinatorLayout_2
結合 CollapsingToolbarLayout,向上滑動時 ImageView 會收合、Toolbar 隨之浮現,向下滑動則相反,呈現類似折疊展開的視覺效果。
在 CollapsingToolbarLayout 中,透過app:contentScrim="?attr/colorPrimary"屬性設定 Toolbar 收合到頂部後的背景色,同時設定了app:layout_scrollFlags="scroll|exitUntilCollapsed"屬性,其中 scroll 的含義前面已解釋過,exitUntilCollapsed 的含義如下:
exitUntilCollapsed:滾動退出畫面後,最終收合固定在頂端。
此外,AppBarLayout 的高度需要設定為一個固定值,這樣 CollapsingToolbarLayout 才能產生折疊效果。
收合後 Toolbar 能固定在頂部,靠的是app:layout_collapseMode="pin"屬性;除了 pin,還有另一個可選值 parallax:
pin:固定模式,折疊時最終固定在頂端。
parallax:視差模式,折疊時會產生視差效果。
同時在 ImageView 上使用了app:layout_collapseMode="parallax"屬性來製造視差漸變,並用app:layout_collapseParallaxMultiplier="0.7"控制視差幅度,屬性值範圍為 [0.0, 1.0],數值越大視差越明顯。
最後,別忘了在 NestedScrollView 中設定app:layout_behavior="@string/appbar_scrolling_view_behavior"屬性。
3、CoordinatorLayout + FloatingActionButton
版面配置程式碼如下:
<android.support.design.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> ....................... <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="16dp" android:src="@android:drawable/ic_menu_share" /> </android.support.design.widget.CoordinatorLayout>
當 FloatingActionButton 的父 View 是 CoordinatorLayout 時,它會顯示在畫面右下角,在 Java 程式碼中可以這樣使用:
mFab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(mCoordinatorLayout, "點我分享哦!", Snackbar.LENGTH_SHORT).show(); } });看效果:

CoordinatorLayout_3
當 Snackbar 出現時,FloatingActionButton 會自動上移,消失時則下移。但如果 FloatingActionButton 的父 View 是 RelativeLayout 或其他容器,它就無法上移,反而會被 Snackbar 遮蓋住。
在 CoordinatorLayout_2 的 gif 效果中,上滑時 FloatingActionButton 會跟著收合消失,其實只需要以下兩個屬性就能辦到:
app:layout_anchor="@id/app_bar" app:layout_anchorGravity="bottom|end"
第一個屬性讓 FloatingActionButton 錨定在 AppBarLayout 的區域,第二個屬性則決定具體的顯示位置。
二、Toolbar
使用 Toolbar 前,得先隱藏原有的 ActionBar,我們可以新增一個主題:
<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
也可以直接擴充原有的主題:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
colorPrimary:對應 ActionBar 的顏色。
colorPrimaryDark:對應狀態列的顏色。
colorAccent:對應 EditText 編輯時、FloatingActionButton 背景等處的顏色。
版面配置中的用法可參考上面的程式碼,接著在 Java 程式碼中取得 Toolbar,並透過setSupportActionBar(mToolbar); 讓 Toolbar 取代原本的 ActionBar。
接下來看一張圖:

Toolbar
圖中標註了幾個 Toolbar 常用的擴充方法,方便我們依需求客製化自己的 Toolbar,更具體的實作可參考原始碼。
三、NavigationView
先來看效果:

NavigationView
其實 NavigationView 就是用來實作我們常見側滑選單效果的。
版面配置程式碼如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <include layout="@layout/content_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:menu="@menu/menu_drawer" app:headerLayout="@layout/nav_head_main" /> </android.support.v4.widget.DrawerLayout>
以 DrawerLayout 作為根配置,content_main 是主畫面配置,最後是 NavigationView,透過android:layout_gravity="start"設為左側滑選單,同理android:layout_gravity="end"代表右側滑選單。
其中
app:menu="@menu/menu_drawer" app:headerLayout="@layout/nav_head_main"
這兩個屬性分別對應 NavigationView 頂部的 header 配置與下方的 menu item 配置,細節可參考原始碼。
準備好版面配置後,接下來就是初始化工作:
private void initNavigationView() { //初始化NavigationView頂部head的icon和name ImageView icon = (ImageView) mNavView.getHeaderView(0).findViewById(R.id.nav_head_icon); icon.setImageResource(R.mipmap.ic_launcher); TextView name = (TextView) mNavView.getHeaderView(0).findViewById(R.id.nav_head_name); name.setText("VipOthershe"); //設置NavigationView對應menu item的點擊事情 mNavView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.nav_item1: break; case R.id.nav_item2: break; case R.id.nav_set: break; case R.id.menu_share: break; case R.id.nav_about: break; } //隱藏NavigationView mDrawerLayout.closeDrawer(GravityCompat.START); return true; } }); }很簡單,就是初始化 NavigationView 的頂部 header 與 Menu Item。
接下來看看如何把側滑選單和 Toolbar 結合使用:
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.addDrawerListener(toggle); //設置左上角顯示三道橫線 toggle.syncState();
透過這段設定把兩者綁定在一起,同時 Toolbar 左上角會顯示三道橫線,點擊左上角即可開啟側滑選單。
還可以采用另一種方式:
//設置Toolbar左上角圖標 mToolbar.setNavigationIcon(R.mipmap.ic_launcher); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mDrawerLayout.openDrawer(GravityCompat.START); } });先設定 Toolbar 左上角圖示並綁定事件,再透過mDrawerLayout.openDrawer(GravityCompat.START); 開啟側滑選單。
以上兩個方法分別對應開啟與關閉側滑選單:
mDrawerLayout.openDrawer(GravityCompat.START); mDrawerLayout.closeDrawer(GravityCompat.START);
若要改用右側滑選單,把 GravityCompat.START 換成 GravityCompat.END 即可。
以上就是 Android 中 Material Design 風格相關控件的整理,文章透過實例做了詳細介紹,希望對大家在開發 App 時有所幫助。
Android 實作:打造可拖曳、雙擊關閉的懸浮視窗
本文示範如何實作一個可拖曳移動、雙擊後消失的懸浮視窗,提供給有相同需求的開發者參考。 實作目標:點擊按鈕後關閉目前的 Activity 並顯示懸浮視窗,視窗可自由拖動,雙
一加手機 3 怎麼截圖?三種截螢幕方式整理
一加手機 3 是發表不久的旗艦機,延續了前代的高性價比路線,頗受歡迎。不過新機上手時,總有些基本操作需要確認一下,例如截圖。以下整理三種做法,依場合挑最順手的用。 方法
Android 取得時間戳並轉換為標準時間格式
在實際開發中,伺服器回傳的時間往往採用時間戳(timestamp)格式。這時用戶端就需要將時間戳進一步處理,轉換成人類可讀的標準時間格式。本文示範如何在 Android
Android Activity(活動)全面詳解
Activity 是 Android 四大元件之一,幾乎每一個可見的應用介面背後都有一個 Activity 在支撐。本文將從專案結構、日誌工具一路談到 Activity