學(xué)習(xí)目的
- 學(xué)會(huì)使用
DrawerLayout
- 學(xué)會(huì)使用
NavigationView
- 學(xué)會(huì)使用ToolBar+DrawerLayout+NavigationView實(shí)現(xiàn)側(cè)滑抽屜效果
- 學(xué)會(huì)實(shí)現(xiàn)Toolbar在頂部以及Toolbar被遮擋的兩種效果
效果展示
側(cè)滑控件我們?cè)陂_發(fā)的過程中經(jīng)常用到,廢話不多說,先上圖:
實(shí)現(xiàn)過程
如果要實(shí)現(xiàn)上圖的展示效果,需要先創(chuàng)建個(gè)BaseActivit類,這個(gè)類里面包含Toolbar的初始化,然后再創(chuàng)建DrawerLayout相關(guān)的Activity
創(chuàng)建BaseActivity及相關(guān)XML文件
activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:contentInsetStartWithNavigation="0dp"
android:theme="@style/ToolBarStyle"
app:titleTextColor="@color/white">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</LinearLayout>
BaseActivity
package com.vv.drawerlayout.ui.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import com.vv.drawerlayout.R;
import butterknife.ButterKnife;
/**
* @author ShenZhenWei
* @date 2018/7/4
*/
public abstract class BaseActivity extends AppCompatActivity {
protected Toolbar mToolbar;
protected FrameLayout mContainerLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mToolbar = findViewById(R.id.toolbar);
mContainerLayout = findViewById(R.id.frameLayout);
boolean isToobar = initToolbar();
if (isToobar) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNavigationClick();
}
});
} else {
mToolbar.setVisibility(View.GONE);
}
initContent(getLayoutId());
}
private void initContent(int layoutId) {
if (layoutId != 0) {
View contentView = LayoutInflater.from(this).inflate(layoutId, mContainerLayout, false);
mContainerLayout.addView(contentView);
ButterKnife.bind(this);
initViews();
}
}
/**
* 初始化布局
*/
protected abstract void initViews();
/**
* 獲取布局控件
*
* @return
*/
protected abstract int getLayoutId();
private void onNavigationClick() {
finish();
}
/**
* 初始化Toolbar
*
* @return
*/
protected abstract boolean initToolbar();
}
創(chuàng)建DrawerLayout相關(guān)的Activity以及相關(guān)的XML文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="left|start"
app:headerLayout="@layout/header_layout"
app:menu="@menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
header_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:background="@color/_0091ea"
android:orientation="vertical"
>
<ImageView
android:id="@+id/img_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="@color/white"
android:text="" />
</LinearLayout>
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_favorite_article"
android:icon="@drawable/ic_favorite_gray_24dp"
android:title="@string/favorite_article"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_about"
android:icon="@drawable/ic_about_us_gray_24dp"
android:title="@string/about_us"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_exit"
android:icon="@drawable/ic_exit_to_app_gray_24dp"
android:title="@string/exit"
app:showAsAction="ifRoom"
/>
</menu>
- 如果使用Toolbar,那么需要把系統(tǒng)的主題換成沒有ActionBar的主題,修改
style.xml
文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme" parent="AppTheme.Base"></style>
<style name="ToolBarStyle" parent="@style/BaseToolBarStyle"></style>
</resources>
-
MainActivity
實(shí)現(xiàn)
package com.vv.drawerlayout.ui;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.vv.drawerlayout.R;
import com.vv.drawerlayout.ui.base.BaseActivity;
import butterknife.Bind;
/**
* @author ShenZhenWei
*/
public class MainActivity extends BaseActivity {
@Bind(R.id.navigation_view)
NavigationView mNavigationView;
@Bind(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
mToggle.syncState();
mDrawerLayout.addDrawerListener(mToggle);
mNavigationView.setItemIconTintList(null);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return false;
}
});
}
@Override
protected void initViews() {
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected boolean initToolbar() {
mToolbar.setTitle(R.string.home);
mToolbar.setNavigationIcon(R.drawable.ic_menu_black_24dp);
return true;
}
}
- 運(yùn)行查看效果
優(yōu)化
但是現(xiàn)在我們發(fā)現(xiàn)側(cè)邊欄是沒有選中狀態(tài)的,如效果圖所示,
我們?cè)趺床拍苁亲筮叺膶?dǎo)航欄有選擇狀態(tài)呢?
給左邊的導(dǎo)航欄加上選中效果
- 首先在布局文件中,
NavigationView
的控件的meun
中注明一個(gè)behavior
<group
android:checkableBehavior="single">
<item
android:title="@string/home">
</item>
</group>
android:checkableBehavior="single"
表示每一項(xiàng)都是單選,如果不標(biāo)明的話,NavigationView不會(huì)有選中效果.
- 在Java文件中增加監(jiān)聽器
mNavigationView.setNavigationItemSelectedListener(onNavigationItemSelectedListener);
NavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_favorite_article:
break;
case R.id.menu_about:
break;
case R.id.menu_exit:
break;
case R.id.menu_home:
break;
default:
break;
}
//關(guān)閉側(cè)邊欄
mDrawerLayout.closeDrawers();
return true;
}
};
- 運(yùn)行效果
改變圖標(biāo)的顏色
我們給剛剛增加的NavigationView
的控件的meun
的Item增加一個(gè)icon
<group
android:checkableBehavior="single">
<item
android:id="@+id/menu_home"
android:icon="@mipmap/ic_launcher"
android:title="@string/home">
</item>
</group>
ok看下現(xiàn)在的效果:
5我們發(fā)現(xiàn)首頁
旁邊的圖標(biāo)變成了黑色.這并不是我們想要的.
我們?cè)趺慈ジ腎con的顏色呢,有兩種方法:
- 在NavigationView的布局xml文件里增加
app:itemIconTint="@color/xxx"
屬性,可以設(shè)置Icon的圖片顏色為統(tǒng)一的一種顏色
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="left|start"
app:headerLayout="@layout/header_layout"
app:menu="@menu/navigation_menu"
app:itemIconTint="@color/_0091ea">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
運(yùn)行效果如下:
我們可以看到所有的Icon的圖片顏色全部換成了統(tǒng)一的藍(lán)色.但是首頁旁邊的Icon仍不是我們想要的圖標(biāo)顏色.這就得需要第二種方法了.
- 我們?nèi)绻胱孖con顯示本省的顏色,就得在java文件里面調(diào)用以下方法:
mNavigationView.setItemIconTintList(null);
MainActivity
package com.vv.drawerlayout.ui;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.vv.drawerlayout.R;
import com.vv.drawerlayout.ui.base.BaseActivity;
import butterknife.Bind;
/**
* @author ShenZhenWei
*/
public class MainActivity extends BaseActivity {
@Bind(R.id.navigation_view)
NavigationView mNavigationView;
@Bind(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
mToggle.syncState();
mDrawerLayout.addDrawerListener(mToggle);
mNavigationView.setItemIconTintList(null);
mNavigationView.setNavigationItemSelectedListener(onNavigationItemSelectedListener);
}
@Override
protected void initViews() {
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected boolean initToolbar() {
mToolbar.setTitle(R.string.home);
mToolbar.setNavigationIcon(R.drawable.ic_menu_black_24dp);
return true;
}
NavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_favorite_article:
break;
case R.id.menu_about:
break;
case R.id.menu_exit:
break;
case R.id.menu_home:
break;
default:
break;
}
//關(guān)閉側(cè)邊欄
mDrawerLayout.closeDrawers();
return true;
}
};
}
運(yùn)行效果:
可以看到,這個(gè)效果就是我們想要的~
改變Item的背景,改變Item的TextColor
- 如果要改變Item的背景,我們需要用到
NavigationView
的.app:itemBackground=""
方法 - 如果要改變Item的TextColor,我們需要用到
NavigationView
的app:itemTextColor=""
方法
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="left|start"
app:headerLayout="@layout/header_layout"
app:menu="@menu/navigation_menu"
app:itemBackground="@color/_0091ea"
app:itemTextColor="@color/white"
app:itemIconTint="@color/_0091ea">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
效果如下:
給Item之間增加分割線
我們只需要在以上基礎(chǔ)上增加一個(gè)group_id即可.即:講menu先放入到group組中,然后再給這個(gè)group賦上id的值,就能實(shí)現(xiàn)分割線效果.
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/menu_favorite_article"
android:icon="@drawable/ic_favorite_gray_24dp"
android:title="@string/favorite_article"
app:showAsAction="ifRoom"
app:itemIconTint="@color/_0091ea"/>
<item
android:id="@+id/menu_about"
android:icon="@drawable/ic_about_us_gray_24dp"
android:title="@string/about_us"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_exit"
android:icon="@drawable/ic_exit_to_app_gray_24dp"
android:title="@string/exit"
app:showAsAction="ifRoom"
/>
<group
android:id="@+id/group_home"
android:checkableBehavior="single"
>
<item
android:id="@+id/menu_home"
android:icon="@mipmap/ic_launcher_round"
android:title="@string/home">
</item>
</group>
</menu>
運(yùn)行效果
怎么實(shí)現(xiàn)Toolbar被遮擋的側(cè)滑抽屜效果
我們常見的應(yīng)用,像網(wǎng)易云音樂,QQ等都是Toolbar被遮擋的側(cè)滑效果.
我們實(shí)現(xiàn)這種效果就得讓DrawerLayout
包含在Toolbar
外面
activity_drawer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
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:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/home"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header_layout"
app:itemIconTint="@color/colorAccent"
app:menu="@menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
package com.vv.drawerlayout.ui;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.vv.drawerlayout.R;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* @author ShenZhenWei
* @date 2018/7/5
*/
public class DrawerLayoutActivity extends AppCompatActivity {
@Bind(R.id.toolbar)
Toolbar mToolbar;
@Bind(R.id.navigation_view)
NavigationView mNavigationView;
@Bind(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
ButterKnife.bind(this);
//設(shè)置Toolbar
setToolbar();
//設(shè)置DrawerToggle 開關(guān)
setDrawerToggle();
//設(shè)置監(jiān)聽器
setListener();
}
private void setListener() {
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mDrawerLayout.closeDrawers();
return true;
}
});
}
private void setDrawerToggle() {
mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mToggle);
//同步DrawerLayout的狀態(tài)
mToggle.syncState();
}
private void setToolbar() {
setSupportActionBar(mToolbar);
// 顯示Home的圖標(biāo)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
注意:
setSupportActionBar(mToolbar);
一定要寫在mDrawerLayout.addDrawerListener(mToggle);
之前,否則將無法響應(yīng)Home頁面上的Icon點(diǎn)擊事件.
運(yùn)行效果:
代碼
為了方便,代碼已經(jīng)上傳GitHub,敬請(qǐng)下載~