java安卓侧边栏收起_Android侧边栏的自定义实现(附源码)

本文介绍Android侧边栏的自定义实现,要实现手指在手机上向左或向右移动时,能相应的移动左右两个视图。通过自定义来实现,不借助第三方插件。实现的思路很简单,通过判断手指滑动的距离和速度来决定是否要滚动显示菜单项.(左边图片)

先来看看效果:

aa84017da0a3076b965ac543ebd91d39.gif

目录:

一、实现思路

二、代码清单

三、效果与说明

本文源码下载

------------------------------------------分割线------------------------------------------

具体下载目录在 /2015年资料/2月/18日/Android侧边栏的自定义实现(附源码)/

------------------------------------------分割线------------------------------------------

下面,让我们开始吧:

一、实现思路

1.思路

菜单在左,内容在右,然后菜单显示时和手机右边框有一定的间隔,内容显示一小部分。内容全部显示时,菜单全部不可见。如下面两个图

131e254d29a3f02237e46c9f5d115ede.png

显示内容

6cf7dc1403c0e78678513da3b07fadf9.png

显示菜单

2.判断逻辑

这是判断手指按着屏幕和手指抬起时要不要显示还是隐藏菜单

5cdf4eedf3688b2e299f8b3dfb02166c.png

二、代码清单

首先来看下布局:

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context=".MainActivity" >

android:id="@+id/menu"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:background="@drawable/pn" >

android:id="@+id/content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:background=

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用程序中实现侧边,可以使用 NavigationView 和 DrawerLayout 组件。 以下是实现侧边的基本步骤: 1. 首先,在你的布局文件中添加 DrawerLayout 组件,并将其作为根视图。 ``` <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> //主要内容区域 <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> //侧边 <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header" app:menu="@menu/nav_menu" /> </android.support.v4.widget.DrawerLayout> ``` 2. 在 Activity 中,将 NavigationView 组件与 DrawerLayout 组件进行关联,并设置 toggle 按钮。 ``` DrawerLayout drawer = findViewById(R.id.drawer_layout); NavigationView navigationView = findViewById(R.id.nav_view); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); navigationView.setNavigationItemSelectedListener(this); ``` 3. 创建菜单项和头部布局。 在 res/menu 目录下创建 nav_menu.xml 文件,并在其中定义菜单项。 ``` <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="@string/nav_home" /> <item android:id="@+id/nav_messages" android:icon="@drawable/ic_message" android:title="@string/nav_messages" /> <item android:id="@+id/nav_settings" android:icon="@drawable/ic_settings" android:title="@string/nav_settings" /> </group> </menu> ``` 在 res/layout 目录下创建 nav_header.xml 文件,并在其中定义头部布局。 ``` <ImageView android:id="@+id/nav_header_imageView" android:layout_width="match_parent" android:layout_height="180dp" android:scaleType="centerCrop" android:src="@drawable/header_image" /> <TextView android:id="@+id/nav_header_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="8dp" android:text="Your Name" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> ``` 4. 处理菜单项点击事件。 在 Activity 中实现 NavigationView.OnNavigationItemSelectedListener 接口,并处理菜单项点击事件。 ``` @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_home) { // Handle the home action } else if (id == R.id.nav_messages) { // Handle the messages action } else if (id == R.id.nav_settings) { // Handle the settings action } DrawerLayout drawer = findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } ``` 这样就可以实现一个基本的侧边了。当用户从屏幕的左边缘向右滑动时,侧边会出现在屏幕上。用户可以点击菜单项或者在内容区域滑动以关闭侧边

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值