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

安卓侧滑菜单怎么设置

发布时间: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 表示菜单项的文字描述。

阅读全文

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

热点内容
java两个数的差 浏览:919
编程语言性能排行 浏览:336
深度学习推理引擎编译 浏览:515
cdr怎么转pdf格式 浏览:405
蒙商信用卡app里怎么修改收货地址 浏览:331
php高级面试题2017 浏览:937
紫色可以缓解压力吗 浏览:681
内核编译版本cpu架构 浏览:574
中级程序员面试题 浏览:162
在线压缩音乐文件 浏览:746
php删除非空文件夹 浏览:3
php你好世界代码 浏览:818
k270cz1压缩机功率 浏览:321
不知从哪里开始学习python 浏览:427
java打开文件方式 浏览:703
keil分段编译 浏览:27
app有了用户后怎么赚钱 浏览:306
程序员那么可爱20观看 浏览:999
一个数除以二的算法 浏览:403
如何选择php培训机构 浏览:982