Android 使用Toolbar+DrawerLayout快速实现仿“知乎APP”侧滑导航效果

简介:

  在以前,做策划导航的时候,最常用的组件便是SlidingMenu了,当初第一次用它的时候觉得那个惊艳啊,体验可以说是非常棒。 后来,Android自己推出了一个可以实现策划导航的组件DrawerLayout,也相当的不错,更加简洁,好使。当前,很多的APP都会采用侧滑导航的设计,不仅体验上很好,而且这种为APP节省了很多“空间”的交互,给人的感觉更加舒服。

 Android已经越来越追求用户体验,在APP功能越来越成熟稳定的情境下,把用户体验做到极致,是开发者应有的追求!

 除了DrawerLayout的设计外,Android还推出了Toolbar,用于取代传统的ActionBar,这是一种符合Material Design设计规范的组件,越来越被更多的开发者接受,并应于自己的项目中,如最新的“知乎”客户端,就采用该种设计,体验的感觉非常好。

 关于DrawerLayout和Toolbar分别是如何使用,原理又是什么,我不在此处过多介绍,不会的或者有兴趣的人可以参考几篇不错的博文:

1. ANDROID – TOOLBAR STEP BY STEP

2. android官方侧滑菜单DrawerLayout详解


  我要介绍的,是如何使用Toolbar和DrawerLayout快速侧滑导航,先看下接下来要实现的效果:


1. 首先,新建项目,并在buile.gradle中添加appcompat-v7支持。

dependencies {
    compile 'com.android.support:appcompat-v7:23.1.1'
}


2. 创建ToolBar标题栏布局layout_tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">

</android.support.v7.widget.Toolbar>

3. 创建 DrawerLayout侧滑页面layout_custom_drawer.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/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--主布局-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:background="#00c7c0"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="主页面"
            android:textSize="40sp"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>
    <!--侧滑菜单-->
    <LinearLayout
        android:id="@+id/drawerContent"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="#F5F5F5"
        android:orientation="vertical"
        android:layout_gravity="start">

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="我的收藏"
            android:gravity="center"
            android:textSize="16sp"
            />

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_height="50dp"
            android:text="我的关注"
            android:gravity="center"
            android:textSize="16sp"
            />

    </LinearLayout>

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

4. 在主页面中,引用上面的ToolBar布局及 DrawerLayout布局activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--Toolbar-->
    <include layout="@layout/layout_tool_bar" />
    <!--DrawerLayout-->
    <include layout="@layout/layout_custom_drawer" />
</LinearLayout>


5. 编写Activity代码,控制 ToolBar及 DrawerLayout相应的逻辑

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar toolbar;

    private DrawerLayout drawerLayout;

    private LinearLayout drawerContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerContent = (LinearLayout) findViewById(R.id.drawerContent);
        // 设置Toolbar
        toolbar.setTitle("掌阅宝");
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));

        // 设置toolbar支持actionbar
        setSupportActionBar(toolbar);

        // 使用ActionBarDrawerToggle,配合DrawerLayout和ActionBar,以实现推荐的抽屉功能。
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
        mDrawerToggle.syncState();
        drawerLayout.setDrawerListener(mDrawerToggle);


        TextView text1 = (TextView) findViewById(R.id.text1);
        TextView text2 = (TextView) findViewById(R.id.text2);

        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 关闭DrawerLayout
        drawerLayout.closeDrawer(drawerContent);
        switch (v.getId()) {

            case R.id.text1:

                Toast.makeText(MainActivity.this, "我的收藏", Toast.LENGTH_SHORT).show();
                break;
            case R.id.text2:
                Toast.makeText(MainActivity.this, "我的关注", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}


代码中注释已经够简单明了,关于ActionBarDrawerToggle是什么,可参考《 ActionBarDrawerToggle的简要介绍》。


6. 配置系统的theme

<resources>

    <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>

        <!--返回键样式-->
        <item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>

        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <!-- 使用 API Level 22编译的話,要拿掉前綴字 -->
        <item name="windowNoTitle">true</item>
    </style>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base"></style>

    <style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
        <item name="color">@android:color/white</item>
    </style>

</resources>

通过以上六步,你就可以使用Toolbar+DrawerLayout来快速实现类似“知乎APP”侧滑导航效果了。


源码下载地址:https://github.com/zuiwuyuan/DrawerLayout_ToolBar



如此这般,就OK啦!欢迎互相学习!
如有疑问,欢迎留言探讨。

如此这般,就OK啦!欢迎互相学习!
如有疑问,欢迎留言探讨。
相关文章
|
1月前
uni-app 4.4封装头部导航组件(二)
uni-app 4.4封装头部导航组件(二)
12 0
|
1月前
uni-app头部导航组件开发(4.1)
uni-app头部导航组件开发(4.1)
16 0
|
3月前
|
Android开发 开发者 iOS开发
APP开发后如何上架,上架Android应用市场前要准备什么
移动应用程序(APP)的开发已经成为现代企业和开发者的常见实践。然而,开发一个成功的APP只是第一步,将其上架到应用商店让用户下载和使用是实现其潜力的关键一步。
|
20天前
|
Android开发
Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果
Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果
10 0
|
1月前
|
设计模式 测试技术 数据库
基于Android的食堂点餐APP的设计与实现(论文+源码)_kaic
基于Android的食堂点餐APP的设计与实现(论文+源码)_kaic
|
1月前
|
Android开发
【Android 从入门到出门】第四章:现代Android开发中的导航
【Android 从入门到出门】第四章:现代Android开发中的导航
22 2
【Android 从入门到出门】第四章:现代Android开发中的导航
|
1月前
uni-app 4.3封装头部导航组件(一)
uni-app 4.3封装头部导航组件(一)
12 0
|
2月前
|
安全 Java 数据挖掘
当 App 有了系统权限,真的可以为所欲为? Android Performance Systrace
当 App 有了系统权限,真的可以为所欲为? Android Performance Systrace 转载自: https://androidperformance.com/2023/05/14/bad-android-app-with-system-permissions/#/0-Dex-%E6%96%87%E4%BB%B6%E4%BF%A1%E6%81%AF
30 0
|
3月前
|
Android开发
闲暇时间收集和整理的Android的一些常用的App
闲暇时间收集和整理的Android的一些常用的App
14 0