导航:首页 > 操作系统 > 安卓侧滑菜单怎么设置

安卓侧滑菜单怎么设置

发布时间:2025-01-04 09:48:30

‘壹’ androidQQ侧滑菜单代码

下面是一个简单的 Android QQ 侧滑菜单代码实现示例:

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;

private ActionBarDrawerToggle toggle;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawerLayout = findViewById(R.id.drawer_layout);

toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);

drawerLayout.addDrawerListener(toggle);

toggle.syncState();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

NavigationView navigationView = findViewById(R.id.nav_view);

navigationView.(new NavigationView.() {

@Override

public boolean onNavigationItemSelected(@NonNull MenuItem item) {

// 处理菜单项点击事件

drawerLayout.closeDrawer(GravityCompat.START);

return true;

}

});

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if (toggle.onOptionsItemSelected(item)) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

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

<androidx.drawerlayout.widget.DrawerLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

<!-- 主布局 -->

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

<!-- 添加主界面的控件 -->

</RelativeLayout>

<!-- 侧滑菜单布局 -->

<com.google.android.material.navigation.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" />

</androidx.drawerlayout.widget.DrawerLayout>

其中,@layout/nav_header 表示侧滑菜单的头部布局,@menu/nav_menu 表示侧滑菜单的菜单项布局。

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

<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/menu_home" />

<item

android:id="@+id/nav_gallery"

android:icon="@drawable/ic_gallery"

android:title="@string/menu_gallery" />

<item

android:id="@+id/nav_slideshow"

android:icon="@drawable/ic_slideshow"

android:title="@string/menu_slideshow" />

</group>

</menu>

其中,@drawable/ic_XXX 表示菜单项的图标,@string/menu_XXX 表示菜单项的文字描述。

阅读全文

与安卓侧滑菜单怎么设置相关的资料

热点内容
cmd编译错误找不到符号 浏览:695
linuxip机器名 浏览:487
服务器喇叭和频道喇叭有什么区别 浏览:31
闲鱼排名算法如何计算 浏览:975
linuxtargz解压命令 浏览:741
还完房贷后解压手续银行会代办吗 浏览:811
解压烹饪乐高 浏览:675
元神的服务器怎么看 浏览:362
stc8单片机串口中断 浏览:954
信号分析pdf 浏览:927
暴力删除命令 浏览:803
qt如何编译加快速度 浏览:903
php添加数据sql语句 浏览:717
免费的小说app有什么 浏览:405
螺杆压缩机进气阀动画 浏览:651
两台服务器如何做负载均衡 浏览:227
程序员的工资是涨的吗 浏览:813
视频存储服务器可以干什么 浏览:463
创建文件夹安装失败怎么回事 浏览:832
程序员高考隔了几年 浏览:822