栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何添加导航抽屉?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何添加导航抽屉?

尝试这样的操作,对于布局文件,您只需要

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    tools:openDrawer="start"><!-- Main content when drawer is closed -->    <include        layout="@layout/app_bar_nav"        android:layout_width="match_parent"        android:layout_height="match_parent" /><!-- The drawer, you can change the menu contents dynamically -->    <android.support.design.widget.NavigationView        android:id="@+id/nav_view"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_gravity="start"        android:fitsSystemWindows="true"        app:headerLayout="@layout/nav_header"        app:menu="@menu/menu_nav" /></android.support.v4.widget.DrawerLayout>

实现可以像这样简单。

package com.example;public class NavigationDrawer extends AppCompatActivity        implements NavigationView.onNavigationItemSelectedListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_navigation_drawer);        //You should remove this if you have no intent of using it        //And if you uset it, to prevent double actionbars, use a style with no actionbar        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);//I like setting custom actionbar        //setSupportActionBar(toolbar);        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(     this, drawer, toolbar, "Open drawer", "Close drawer");        drawer.setDrawerListener(toggle);        toggle.syncState();        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);        navigationView.setNavigationItemSelectedListener(this);    }    @Override    public void onBackPressed() {        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);        if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START);        } else { super.onBackPressed();        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.note_home, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        return true;//or super.onOptionsItemSelected, false won't show menu    }    @SuppressWarnings("StatementWithEmptyBody")    @Override    public boolean onNavigationItemSelected(MenuItem item) {        // Handle navigation view item clicks here.        int id = item.getItemId();        switch (id) { case R.id.nav_camera:     break; case R.id.nav_gallery:     break; case R.id.nav_schedule:     break; case R.id.nav_manage:     //do someting silly     break;        }        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);        drawer.closeDrawer(GravityCompat.START);        return true;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号