Google在2015的IO大會上,給我們帶來了更加詳細的Material Design設計規范,同時,也給我們帶來了全新的Android Design Support Library,Android Design Support Library的兼容性更廣,直接可以向下兼容到Android 2.2。
下面介紹design Libraay,部分內容出自官方文檔。
英文原文: http://android-developers.blogspot.jp/2015/05/android-design-support-library.html
翻譯: http://www.jcodecraeer.com/a/anzhuokaifa/developer/2015/0531/2958.html
使用design,首先我們要先引入它的Libray包。如果使用AS開發只需要在build.gradle文件里面添加:
compile 'com.android.support:design:23.2.0' 目前的最新版本是23.2.0
一 SnackBar
snackbar顯示在屏幕底部,包含文字信息和可選的操作按鈕,是提供快速反饋的好控件,在指定時間結束之后消失,用戶也可以在達到設置時間之前將它滑動刪除,因為包含可以滑動刪除和交互按鈕,所以snackbar比Toast更加強大。下面看一下SnakBar的api:
Snackbar
.make(parentLayout, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action, myOnClickListener)
.show(); // Don’t forget to show!
二 Navigation View
現在大多數的都存在側滑欄,NavigationView的使用,是側滑欄的設計更加簡單,尤其是對於剛剛學習使用側滑欄的用戶,上面的側滑欄里面的內容就是通過NavigationView添加的,我們可以吧NavigationView和DrawerLayout結合使用,來實現側滑效果。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true">
<!-- your content layout --> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/navigation_drawer_header" app:menu="@menu/navigation_drawer_menu" /> </android.support.v4.widget.DrawerLayout>
我們注意兩個屬性:app:headerLayout--控制側滑欄頭部的布局;app:menu 導航菜單的資源文件(也可以再運行時配置),下面分別貼出兩個xml的碼:
navigation_drawer_header.XML ,用來設置側滑欄頭部的顯示布局,這里只是展示了一個圖片。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="200dp" android:background="#512da8"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_centerInParent="true" /> </RelativeLayout>
navigation_drawer_menu.XML 用來設置側護欄的內容部分。
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/item_green" android:icon="@mipmap/ic_launcher" android:title="Green" /> <item android:id="@+id/item_blue" android:icon="@mipmap/ic_launcher" android:title="Blue" /> <item android:id="@+id/item_pink" android:icon="@mipmap/ic_launcher" android:title="Pink" /> </group> <item android:title="SubItems2"> <menu> <item android:id="@+id/subitem_01" android:icon="@mipmap/ic_launcher" android:title="SubItem01" /> <item android:id="@+id/subitem_02" android:icon="@mipmap/ic_launcher" android:title="SubItem02" /> <item android:id="@+id/subitem_03" android:icon="@mipmap/ic_launcher" android:title="SubItem03" /> </menu> </item> </menu>
我們通過Navigation調用setNavigationItemSelectedListener,來對側滑欄每個item的點擊添加監聽,然后做自己想做的處理。
三 TextInputLayout:
在design中對默認的EditText也進行了升級,使用TextInputLayout將EditText封裝起來,注意:TextInputLayout一定要和EditText同時使用,不能單獨使用,默認的EditText在使用之前一般我們會設置提示語,但是提示語在輸入一個字母的時候就隱藏了,我們使用TextInputLayout,可以將提示語添加在EditText輸入框的上方。這樣也起到了時刻提示用戶的作用。
具體添加代碼如下:
<android.support.design.widget.TextInputLayout android:id="@+id/til_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.design.widget.TextInputEditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout>
然后在對應界面內設置 textInputLayout.setHint(”your hint");
這樣簡單的幾行代碼就實現了如下效果,對應TextInputLayout個人覺得用處不大,還是很少使用的。
FloatingActionButton
FloatActionButton是一個可以進行點擊的圓形按鈕,如下所示:
<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
早XML文件中,我們可以通過設置FloatActionButton的src來設置里面的圖標。因為FloatActionButton繼承ImageView,所有我們可以使用ImagView里面的所有屬性。但是修改FloatActionButton的按鈕顏色和主題的colorAccent屬性使一致的。現在使用AS開發的都知道,創建一個Blank Activity的時候在生成默認布局時會生成一個FloatActionButton按鈕,一般情況下按鈕時在頁面的右下方,但是我們也可以自定義FlatActionButton的位置。
如上圖所示,我就是把button放在了一個卡片上面,我們可以設置下面這兩個屬性來實行此功能。
app:layout_anchor="@id/cardview1" app:layout_anchorGravity="top|right"
app:layout_anchor屬性來指定FloatActionButton是固定在什么控件或者布局上的。后面的是依附控件的id.
app:layout_anchorGravity屬性指定相對於依附控件的位置。
四 TabLayout(選項卡)
designLibray提出TabLayout之前,我們實現切換功能時,一般會找一些第三方庫或者自己寫,Design的TabLayout實現了固定的選項卡中view的寬度平分,在使用TabLayout時,我們只需要在XML里面添加:
<android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout>
然后在XML對應的界面中添加代碼:
tabLayout.addTab(tabLayout.newTab().setText("全部"));
tabLayout.addTab(tabLayout.newTab().setText("類別A"));
tabLayout.addTab(tabLayout.newTab().setText("類別B"));
tabLayout.addTab(tabLayout.newTab().setText("類別C"));
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.green));
tabLayout.setSelectedTabIndicatorHeight(8);
tabLayout.setTabTextColors(Color.BLACK, getResources().getColor(R.color.green));
后面的Tab名稱自己根據需求添加,這里我就隨機起了幾個title。然后這樣實現的效果就是這樣滴:
setSelectedTabIndicatorColor屬性可以設置選中tab的下划線顏色;
setSelectedTabIndicatorHeight屬性設置下划線的高度
setTabTextColors有兩個屬性,屬性一是默認文字顏色,屬性二是選中的文字顏色。
一般情況下TabLayout適合viewpager配合使用的,viewpager里面可以加載Fragment,如果要和Viewpager同時使用,我們一定要使用setupWithViewPager(viewPager)方法來講TabLayout和viewPager連接在一起。
XML文件如下所示:
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <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" app:popupTheme="@style/AppTheme.PopupOverlay" /> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> </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.v4.view.ViewPager>
在代碼中,我們需要添加一個Adapter,在viewpager中加載內容,我們今天在viewpager中添加Fragment,就創建一個FragmentPagerAdapter:

class Adapter extends FragmentPagerAdapter { private final List<Fragment> mFragments = new ArrayList<>(); private final List<String> mFragmentTitles = new ArrayList<>(); public Adapter(FragmentManager fm) { super(fm); } public void addFragment(Fragment fragment, String title) { mFragments.add(fragment); mFragmentTitles.add(title); } @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { return mFragments.size(); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitles.get(position); } }
然后再調用下面代碼,就完成了,這樣TabLayout和ViewPager就可以結合使用了,是不是簡單了很多呢o(^▽^)o
Adapter adapter = new Adapter(getSupportFragmentManager()); adapter.addFragment(Fragment1.newInstance("one"), "專題1"); adapter.addFragment(Fragment1.newInstance("two"), "專題2"); adapter.addFragment(Fragment1.newInstance("three"), "專題3"); viewPager.setAdapter(adapter); tabLayout.setupWithViewPager(viewPager);
我們來看一下效果圖,就是這樣啦。
注:如果我們的Tab內容不只是3,4個二是更多的時候,TabLayout提供了app:tabMode="scrollable"屬性,使TabLayout可以滑動。
五 CoordinatorLayout
CoordinatorLayout可以看成是一個增強型的FramLayout,我們使用CoordinatorLayout可以實現很多新的操作。
1,CoordinatorLayout與FloatActionButton.
我們吧FloatActionButton作為一個子View添加進入CoordinationLayout中,在觸發FloatActionButton的時候在底部顯示出SnackBar,我們可以看到在SnackBar進入界面的時候,FloatActionButton會自動上移,這就是利用了CoordinationLayout提供的毀掉方法,並且在snackBar消失時回到原來的位置,並且不需要添加額外的代碼。
還有就是我們在上面提到的,CoordinationLayout還提供了layout_anchor屬性和layout_anchorGravity一起使用,可以用於放置懸浮按鈕和其他view的相對位置。這個我們在上面的FloatActionButton已經提到了。
2,CoordinatorLayout與AppBar。
我們看用MD寫的項目幾乎都有使用CoordinatorLayout和ToolBar一起使用實現的滾動技巧。
在添加此功能的時候我們需要添加一個滾動組件,例如RecycleView,NestedScrollView and so on(ScrollView沒有效果).所以我們可以添加一個這樣的布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <! -- Your Scrollable View --> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar ... app:layout_scrollFlags="scroll|enterAlways"> <android.support.design.widget.TabLayout ... /> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>
在上面的代碼中我們呢看到了屬性app:layout_scrollFlags,我們就是通過這個屬性來判斷控件是如何滾出屏幕與滾入屏幕
Flag包括:
① scroll: 所有想滾動出屏幕的view都需要設置這個flag- 沒有設置這個flag的view將被固定在屏幕頂部。
② enterAlways: 這個flag讓任意向下的滾動都會導致該view變為可見,啟用快速“返回模式”。
③ enterAlwaysCollapsed: 顧名思義,這個flag定義的是何時進入(已經消失之后何時再次顯示)。假設你定義了一個最小高度(minHeight)同時enterAlways也定義了,那么view將在到達這個最小高度的時候開始顯示,並且從這個時候開始慢慢展開,當滾動到頂部的時候展開完。
④ exitUntilCollapsed: 同樣顧名思義,這個flag時定義何時退出,當你定義了一個minHeight,這個view將在滾動到達這個最小高度的時候消失。
上面的布局文件中我們在RecycleView中設置了屬性app:layout_behavior="@string/appbar_scrolling_view_behavior",當控件設置了這個屬性,就會觸發設置了app:layout_scrollFlags屬性的控件發生滑動狀態的改變。
注意我們只是CoordinatorLayout和ToolBar舉例說明這個功能,但是CoordinatorLayout也可以和其他控件實現此效果。這個我沒有自己寫例子,就貼上其他Demo的圖:
六 CollapsingToolbarLayout(可折疊的ToolBar)
從字面意思我們就知道這是個可折疊的ToolBar,使用CollapsingToolbarLayout的 CollapsingToolbarLayout繼承FramLayout,CollapsingToolbarLayout包裹ToolBar的時候提供一個可折疊的ToolBar , 一般作為AppBarLayout的子視圖使用。
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="256dp" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" app:contentScrim="?attr/colorPrimary" app:title = "collapsingToolbarLayout" app:layout_scrollFlags="exitUntilCollapsed|scroll"> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@mipmap/cheese_1" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout>
我們早CollapsingToolBarLayout中添加了一個ImageView和ToolBar, CollapsingToolBarLayout有以下幾個基本的屬性。
app:title = "",設置的Title內容在布局展開的時候會變得大些,而在折疊的時候使字體過渡到默認值,注意,我們的title是在CollapsingToolbarLayout上面設置的,而不是在ToolBar上面。
app:contentScrim="?attr/colorPrimary" 設置這個屬性可以是布局在拉伸或者縮小到一定高度的時候漸變ToolBar的顏色,一般漸變的顏色選擇主題色,可以以自定義顏色。
app:layout_collapseMode="" 這個屬性來設置子視圖折疊模式,有兩種pin:
固定模式:app:layout_collapseMode = "pin" 確保Toolbar在view折疊的時候最后固定在屏幕的頂部。
視差模式:app:layout_collapseMode = "parallax" 在折疊的時候會有個視差折疊的效果。
同時我們同時也設置了屬性app:layout_scrollFlags來設置滑動方式,來響應滾動布局的
app:layout_behavior="@string/appbar_scrolling_view_behavior">屬性。來實現滾動布局。
截圖很丑,下面貼上官方的例子:
總結:
寫到這里support design Libray 里面的布局就差不多介紹完了,通過對design的使用感覺還是很人性化的,雖然有些效果的實現在網上有多的第三方庫也可以實現,但是畢竟這是google官方推出的,使用起來更加放心,但是有一個缺點就是部分控件封裝過於嚴重,所以只能在特定的布局或者要求下才能使用。