android g 侧边菜单,DrawerLayout+NavigationView实现侧边滑出菜单,一篇就够了!

引言

在Android开发中的侧边滑出菜单栏。DrawerLayout+NavigationView实现侧边滑出菜单,一篇就够了!

效果预览

35e913f4f586

滑出菜单.gif

用法

第一步:布局文件

(1)主布局:activity_case13.xml

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:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".show.Case13"

tools:ignore="MissingConstraints">

android:layout_width="match_parent"

android:layout_height="60dp"

android:background="@color/green"

android:gravity="center"

android:text="滑出菜单"

android:textColor="@color/white"

android:textSize="20sp" />

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="60dp"

android:background="@color/green"

android:gravity="center"

android:text="滑出菜单"

android:textColor="@color/white"

android:textSize="20sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="20dp"

android:text="内容区"

android:textSize="20sp" />

android:id="@+id/btn_open_left"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="打开左边" />

android:id="@+id/btn_open_right"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="打开右边" />

android:id="@+id/navigation_view"

android:layout_width="260dp"

android:layout_height="match_parent"

app:itemIconTint="@color/green"

android:layout_gravity="start"

app:headerLayout="@layout/drawer_header"

app:menu="@menu/menu_drawer_left"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="end"

android:background="@color/black"

android:gravity="center"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="20dp"

android:text="右边菜单"

android:textColor="@color/white"

android:textSize="20sp"

android:textStyle="bold" />

android:id="@+id/btn_close_right"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="关闭" />

(2)layout下新建:滑出菜单顶部布局draw_header.xml

android:layout_width="match_parent"

android:layout_height="200dp"

android:orientation="vertical">

android:id="@+id/iv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

android:src="@drawable/meizi" />

(3)新建res下的menu文件夹,并新建menu_drawer_left.xml

android:id="@+id/favorite"

android:icon="@drawable/icon"

android:title="我的积分" />

android:id="@+id/wallet"

android:icon="@drawable/icon"

android:title="我的分享" />

android:id="@+id/photo"

android:icon="@drawable/icon"

android:title="我的收藏" />

android:id="@+id/file"

android:icon="@drawable/icon"

android:title="TODO" />

android:id="@+id/setting"

android:icon="@drawable/icon"

android:title="系统设置" />

android:id="@+id/about"

android:icon="@drawable/icon"

android:title="关于我们" />

android:id="@+id/esc"

android:icon="@drawable/icon"

android:title="退出登录" />

第二步:activity处理点击事件

public class Case13 extends AppCompatActivity {

@SuppressLint("WrongConstant")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_case13);

//初始化

DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);

Button btnOpenLeft = findViewById(R.id.btn_open_left);

Button btnOpenRight = findViewById(R.id.btn_open_right);

Button btnCloseRight = findViewById(R.id.btn_close_right);

//点击事件处理

btnOpenLeft.setOnClickListener((View)->{

drawerLayout.openDrawer(Gravity.START);

});

btnOpenRight.setOnClickListener((View)->{

drawerLayout.openDrawer(Gravity.END);

});

btnCloseRight.setOnClickListener((View)->{

drawerLayout.closeDrawer(Gravity.END);

});

//监听事件

drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {

@Override

public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

Log.i("---", "滑动中");

}

@Override

public void onDrawerOpened(@NonNull View drawerView) {

Log.i("---", "打开");

}

@Override

public void onDrawerClosed(@NonNull View drawerView) {

Log.i("---", "关闭");

}

@Override

public void onDrawerStateChanged(int newState) {

Log.i("---", "状态改变");

}

});

}

}

关于menu文件中的图标显示问题:

默认:灰色(会将你的图标色重置为灰色)

设为统一颜色

在com.google.android.material.navigation.NavigationView中设置:

app:itemIconTint="@color/blue"

代码中设为图标原色

navigationView.setItemIconTintList(null)

效果图

35e913f4f586

滑出菜单栏1.jpeg

35e913f4f586

滑出菜单栏2.jpeg

千夜零一:"之前总是看各种博客学习东西,现在我想用博客记录下我的学习脚步,好东西也需要分享,索取和给予是相互的。以后会尽量日更的!目标完成1001篇博客哈哈。”

如果觉得对你有所帮助,请不要吝啬你的点赞,有问题也可以在下方评论区留言哦,关注我一起学习吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值