最详细的 NavigationDrawer 开发实践总结(下)

来源:互联网 发布:什么是java servlet 编辑:程序博客网 时间:2024/05/18 14:45


接上文


仿网易云音乐的 NavigationDrawer 实现


先来看看网易云音乐的效果


云音乐导航菜单


主要就是一个线性布局的菜单并结合了 Translucent System Bar 的特性(还不知道的童鞋请看我前面写的文章哈),下面就直接看看大致实现的布局文件 :


layout/activity_cloud_music.xml


<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/drawer"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@color/color_cd3e3a">

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:fitsSystemWindows="true"

        android:orientation="vertical">

 

        <TextView

            android:layout_width="match_parent"

            android:layout_height="65dp"

            android:background="@color/color_cd3e3a"

            android:gravity="center"

            android:text="网易云音乐"

            android:textColor="@android:color/white"

            android:textSize="18sp" />

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="@android:color/white"

            android:orientation="vertical">

 

        </LinearLayout>

    </LinearLayout>

 

    <LinearLayout

        android:id="@+id/navigation_view"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:background="@android:color/white"

        android:fitsSystemWindows="true"

        android:orientation="vertical">

 

        <ImageView

            android:layout_width="match_parent"

            android:layout_height="180dp"

            android:scaleType="centerCrop"

            android:src="@mipmap/topinfo_ban_bg" />

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="50dp"

            android:gravity="center_vertical"

            android:orientation="horizontal">

 

            <ImageView

                android:layout_width="40dp"

                android:layout_height="40dp"

                android:layout_marginLeft="10dp"

                android:layout_marginRight="10dp"

                android:src="@mipmap/topmenu_icn_msg" />

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="我的消息"

                android:textColor="@android:color/black"

                android:textSize="15sp" />

        </LinearLayout>

 

        ...

        ...

        ...

 

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>


仿造网易云音乐


彩蛋


彩蛋一:左上角的导航动画效果实现


左上角的导航动画


经常会看有些 app 的左上角有这些带感的导航动画,之前想要引入这种效果,都是来自第三方的开源代码,诸如下面两个比较有名的:


  1. LDrawer

  2. android-ui


而现在再也不需要了,Google 推出的 ActionBarDrawerToggle 也能实现这样的效果了,具体查看我在 NavigationDrawerAnimationActivity 中的实现代码


ActionBarDrawerToggle实现效果


如果你对上面这种动画,效果不满意,也可以考虑一下 material-menu 的另一种实现效果。


material-menu动画效果


彩蛋二:比 NavigationView 更好的选择


前面提到 NavigationView 的不实用性,如果你真的要实现 NavigationView那样的效果,又渴望比较高的自由度。这个功能强大且自由度很高的开源库 MaterialDrawer 应该是个很不错的选择。


MaterialDrawer 效果图一


MaterialDrawer 效果图二


总结


到此,对于 NavigationDrawer 的实践总结基本结束。整体给我的感觉是,自从 Material Design 设计开始推出后,Google 推出的这些新控件使用起来更加简单,这能让我们更好的把精力放在编写业务代码上。很多以前需要借助第三方开源库才能实现的效果,现在已经慢慢的不需要了。当然,我们依旧可以去深入的学习这些优秀开源代码,沉淀到更多的干货。这样,小菜也就慢慢成为大牛了。


分享即美德,源代码请看:https://github.com/D-clock/AndroidSystemUiTraining ,本篇的主要实现代码如下红圈所示


主要示例代码

接上文


仿网易云音乐的 NavigationDrawer 实现


先来看看网易云音乐的效果


云音乐导航菜单


主要就是一个线性布局的菜单并结合了 Translucent System Bar 的特性(还不知道的童鞋请看我前面写的文章哈),下面就直接看看大致实现的布局文件 :


layout/activity_cloud_music.xml


<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/drawer"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@color/color_cd3e3a">

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:fitsSystemWindows="true"

        android:orientation="vertical">

 

        <TextView

            android:layout_width="match_parent"

            android:layout_height="65dp"

            android:background="@color/color_cd3e3a"

            android:gravity="center"

            android:text="网易云音乐"

            android:textColor="@android:color/white"

            android:textSize="18sp" />

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="@android:color/white"

            android:orientation="vertical">

 

        </LinearLayout>

    </LinearLayout>

 

    <LinearLayout

        android:id="@+id/navigation_view"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:background="@android:color/white"

        android:fitsSystemWindows="true"

        android:orientation="vertical">

 

        <ImageView

            android:layout_width="match_parent"

            android:layout_height="180dp"

            android:scaleType="centerCrop"

            android:src="@mipmap/topinfo_ban_bg" />

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="50dp"

            android:gravity="center_vertical"

            android:orientation="horizontal">

 

            <ImageView

                android:layout_width="40dp"

                android:layout_height="40dp"

                android:layout_marginLeft="10dp"

                android:layout_marginRight="10dp"

                android:src="@mipmap/topmenu_icn_msg" />

 

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="我的消息"

                android:textColor="@android:color/black"

                android:textSize="15sp" />

        </LinearLayout>

 

        ...

        ...

        ...

 

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>


仿造网易云音乐


彩蛋


彩蛋一:左上角的导航动画效果实现


左上角的导航动画


经常会看有些 app 的左上角有这些带感的导航动画,之前想要引入这种效果,都是来自第三方的开源代码,诸如下面两个比较有名的:


  1. LDrawer

  2. android-ui


而现在再也不需要了,Google 推出的 ActionBarDrawerToggle 也能实现这样的效果了,具体查看我在 NavigationDrawerAnimationActivity 中的实现代码


ActionBarDrawerToggle实现效果


如果你对上面这种动画,效果不满意,也可以考虑一下 material-menu 的另一种实现效果。


material-menu动画效果


彩蛋二:比 NavigationView 更好的选择


前面提到 NavigationView 的不实用性,如果你真的要实现 NavigationView那样的效果,又渴望比较高的自由度。这个功能强大且自由度很高的开源库 MaterialDrawer 应该是个很不错的选择。


MaterialDrawer 效果图一


MaterialDrawer 效果图二


总结


到此,对于 NavigationDrawer 的实践总结基本结束。整体给我的感觉是,自从 Material Design 设计开始推出后,Google 推出的这些新控件使用起来更加简单,这能让我们更好的把精力放在编写业务代码上。很多以前需要借助第三方开源库才能实现的效果,现在已经慢慢的不需要了。当然,我们依旧可以去深入的学习这些优秀开源代码,沉淀到更多的干货。这样,小菜也就慢慢成为大牛了。


分享即美德,源代码请看:https://github.com/D-clock/AndroidSystemUiTraining ,本篇的主要实现代码如下红圈所示


主要示例代码

0 0