今天看了郭神的第二行代碼柬讨,深深的被MaterialDesign的設計風格所吸引馏谨,然后就照例做了一個小Demo故河,希望多多支持,圖片資源來源于網絡彭则。先附上我的源碼地址:https://github.com/xiaweizi/MaterialDesign
照例鳍刷,一波動態(tài)圖來襲:
效果還不錯吧,就是有點失真...其實在這之前Android的UI風格都不是很美觀俯抖,所谷歌的設計工程師們就開始研究出了一種新的設計語言---MaterialDesign输瓜。在2015年得谷歌大會上推出了DesignSupport庫,使得開發(fā)者在即使不了解MaterialDesign的情況下也能非常輕松地將自己得應用Material化芬萍。
接下來我會按照下面流程介紹如何實現(xiàn)這一效果
Toolbar
DrawerLayout
SnackBar
CardView
RecyclerView
SwipeRefreshLayout
AppBarLayout
CollapsingToolbarLayout
沉浸式狀態(tài)欄
首先添加依賴:
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.0.0'
compile'com.android.support:recyclerview-v7:24.0.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:cardview-v7:24.1.1'
compile 'de.hdodenhof:circleimageview:2.1.0'
1. Toolbar
效果如下:
它有點類似于之前得ActionBar尤揣,就是活動最頂部得哪個標題欄。但是柬祠,它只能位于活動得頂部北戏,從而影響效果,所有官方現(xiàn)在已經不再建議使用ActionBar了漫蛔。
1. 設置AppTheme("NoActionBar")
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
...
</style>
2. 添加控件
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/Theme.AppCompat.Light" />
其他屬性我就不說了嗜愈,
app:popupTheme
這個是單獨將彈出得菜單項指定成淡色主題。
這個時候需要指定一個新的命名空間
xmlns:app
這是由于MaterialDesign是在Android5.0系統(tǒng)中才出現(xiàn)得莽龟,而很多Material屬性在5.0之前得系統(tǒng)中是不存在得蠕嫁,那么為了能夠兼容之前得老系統(tǒng),我們就得使用app:
毯盈。
3. 在代碼中使用
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
首先通過
findViewById
得到Toobar實例剃毒,然后調用setSupportActionBar()方法將實例傳入。
4. 設置菜單選項
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/backup"
android:icon="@drawable/ic_backup"
android:title="Backup"
app:showAsAction="always"/>
<item
android:id="@+id/delete"
android:icon="@drawable/ic_delete"
android:title="Delete"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings"
android:title="Settings"
app:showAsAction="never"/>
</menu>
在menu文件夾中創(chuàng)建xml文件
app:showAsAction
來指定按鈕得顯示位置
ifRoom
: 表示屏幕空間足夠得情況下顯示再Toolbar搂赋,不夠就顯示再菜單中
always
: 表示永遠顯示在Toolbar中迟赃,如果屏幕空間不夠則不顯示
never
: 表示永遠顯示在菜單當中
5. 在代碼中實用菜單選項
/***************************
* 創(chuàng)建菜單
***************************/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
/***************************
* 給菜單設置點擊事件
***************************/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.backup:
Toast.makeText(MainActivity.this, "backup", Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
break;
case R.id.settings:
Toast.makeText(this, "setting", Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
2. DrawerLayout
這個在我之前得文章講過,可以直接點擊查看 高大上的側滑菜單DrawerLayout厂镇,解決了不能全屏滑動的問題
不過左側可以再優(yōu)化下纤壁,實用新的控件 NavigationView
NavigationView
在使用前,提前準備好兩個東西:
menu
和headerLayout
1. 創(chuàng)建menu
<?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/nav_call"
android:icon="@drawable/nav_call"
android:title="妹紙" />
<item
android:id="@+id/nav_friends"
android:icon="@drawable/nav_friends"
android:title="段子" />
<item
android:id="@+id/nav_location"
android:icon="@drawable/nav_location"
android:title="新聞" />
<item
android:id="@+id/nav_mail"
android:icon="@drawable/nav_mail"
android:title="本地" />
<item
android:id="@+id/nav_task"
android:icon="@drawable/nav_task"
android:title="收藏" />
</group>
</menu>
將group得
checkableBehavior
屬性指定為single捺信,表示所有得菜單項只能單選
2. 創(chuàng)建headerLayout
<?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="180dp"
android:background="?attr/colorPrimary"
android:padding="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/icon_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:src="@drawable/nav_icon" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="1012126908@qq.com"
android:textColor="#FFF"
android:textSize="14sp" />
<TextView
android:id="@+id/mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/username"
android:text="夏韋子"
android:textColor="#FFF"
android:textSize="14sp" />
</RelativeLayout>
3. 在布局中使用
<android.support.design.widget.NavigationView
android:id="@+id/nv_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu">
</android.support.design.widget.NavigationView>
app:headerLayout
: headerLayout文件
app:menu
: menu文件
android:layout_gravity="left"
設置為左側菜單
4. 在代碼中使用
//給NavigationView設置item選擇事件
mNavigationView.setCheckedItem(R.id.nav_call);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
...
return true;
}
});
3. SnackBar
跟Toast有點相似帽驯,不過不同點在于加入一個可交互按鈕厢塘,當用戶點擊按鈕得時候可以執(zhí)行一些額外得邏輯操作
效果如下:
使用起來比較簡單熙暴,跟Toast很像。
Snackbar snackbar = Snackbar.make(getCurrentFocus(), item.getTitle(), Snackbar.LENGTH_SHORT);
snackbar.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
snackbar.setActionTextColor(Color.BLUE);
snackbar.show();
4. CardView
效果如下:
CardView
其實是一個FrameLayout喇辽,只是額外提供了圓角和陰影效果,
直接在布局中使用雨席。
<android.support.v7.widget.CardView
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="wrap_content"
android:background="?android:attr/selectableItemBackground"
app:cardElevation="10dp"
android:layout_margin="5dp"
app:cardCornerRadius="18dp">
...//子布局
</android.support.v7.widget.CardView>
app:cardCornerRadius
指定卡片圓角得弧度菩咨,數值越大,圓角得弧度越大
app:cardElevation
制定卡片得高度陡厘,高度值越大抽米,投影得范圍越大
5. RecyclerView
這個在之前得文章也說過,如果需要查看糙置,請移駕到: 簡單粗暴----RecyclerView
本次得Demo里數據我就不詳細說了云茸,太多了,很簡單谤饭。
6. SwipeRefreshLayout
效果如下:
使用
SwipeRefreshLayout
直接可以實現(xiàn)下拉刷新的功能
1. 在布局中添加
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
2. 在代碼中添加
mRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN);
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshFruits();
}
});
可以給刷新得時候設置顏色的變換标捺,在
onRefresh()
中實現(xiàn)刷新得功能
7. AppBarLayout
先看一下效果:
AppBarLayout
實際是一個垂直方向得LinearLayout
,它在內部做了很多滾動事件得封裝揉抵,并應用了MaterialDesign設計理念亡容。
在布局中實用:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
android:theme 設置背景主題深色,這樣字體會變成白色
app:popupTheme 設置彈出的主題是亮色
-->
<android.support.design.widget.AppBarLayout
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/Theme.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
...
app:layout_behavior="@string/appbar_scrolling_view_behavior">
...
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
app:layout_scrollFlags
: 當AppBarLayout接收到滾動事件得時候冤今,它內部得子空間就是通過這個屬性影響這些事件的
scroll
:表示當RecyclerView
向上滾動得時候萍倡,Toolbar會跟著一起向上滾動并實現(xiàn)隱藏。
enterAlways
:表示當RecyclerView
向下滾動得時候辟汰,Toolbar會跟著一起向下滾動并重新顯示列敲。
snap
:表示當Toolbar還沒有完全隱藏或顯示得時候,會根據當前滾動得距離帖汞,自動選擇是隱藏還是顯示戴而。
8. CollapsingToolbarLayout
效果如下:
可折疊式標題欄這個就比之前就負責點了,我先貼代碼翩蘸,然后一一解釋所意。
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/fruit_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="35dp"
app:cardCornerRadius="4dp">
<TextView
android:id="@+id/fruit_content_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
app:contentScrim
:在趨于折疊狀態(tài)以及折疊之后得背景色
app:layout_scrollFlags
這個之前講過
scroll
:表示CollapsingToolbarLayout
會隨著妹紙內容詳情得滾動一起滾動
exitUntilCollapsed
:表示當CollapsingToolbarLayout
隨著滾動完成折疊之后就保留在界面上,不再移出屏幕
app:layout_collapseMode="pin"
把Toolbar指定成pin催首,表示在折疊的過程中位置始終保持不變
app:layout_collapseMode="parallax"
ImageView 指定成 parallax扶踊,表示會在折疊得過程中產生一定得錯位偏移,這種模式得視覺效果會非常好
NestedScrollView
:在ScrollView基礎上增加了嵌套響應滾動事件得功能
9. 沉浸式狀態(tài)欄
這個只是在Android5.0后才有的郎任,設置狀態(tài)欄為透明
在setContentView();
之前添加代碼:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
需要在直接子布局添加:android:fitsSystemWindows="true
表示該控件會出現(xiàn)在系統(tǒng)狀態(tài)欄里