這篇文章主要介紹“Android中DrawerLayout怎么實(shí)現(xiàn)側(cè)滑菜單效果”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Android中DrawerLayout怎么實(shí)現(xiàn)側(cè)滑菜單效果”文章能幫助大家解決問(wèn)題。
為贊皇等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及贊皇網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站建設(shè)、贊皇網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
效果圖:
開(kāi)始寫代碼
DrawerLayout 是v4包里面的,所以項(xiàng)目里面需要添加v4包,具體怎么添加就不多說(shuō)了,
NavigationView需要在build.gradle里面添加compile ‘com.android.support:design:25.1.0'
activity_main.xml
然后在res文件夾里面添加一個(gè)menu菜單文件夾,已經(jīng)添加的就不用添加了
上面的代碼里面NavigationView里面有個(gè) app:menu=”@menu/activity_main_drawer”對(duì)應(yīng)如下:
添加一個(gè)菜單文件,名字隨意,我的是activity_main_drawer.xml
activity_main_drawer.xml
再添加一個(gè)布局
上面的代碼里面app:headerLayout=”@layout/nav_header_main”對(duì)應(yīng)如下:
添加一個(gè)布局,名字是nav_header_main.xml
nav_header_main.xml
MainActivity:
public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener { private LinearLayout right; private NavigationView left; private boolean isDrawer=false; private DrawerLayout drawer; @Override public int getLayoutId() { return R.layout.activity_main; } @Override public void initPresenter() { } @Override public void initView() { drawer = (DrawerLayout) findViewById(R.id.drawer_layout); right = (LinearLayout) findViewById(R.id.right); left = (NavigationView) findViewById(R.id.nav_view); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); right.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if(isDrawer){ return left.dispatchTouchEvent(motionEvent); }else{ return false; } } }); drawer.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { isDrawer=true; //獲取屏幕的寬高 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); //設(shè)置右面的布局位置 根據(jù)左面菜單的right作為右面布局的left 左面的right+屏幕的寬度(或者right的寬度這里是相等的)為右面布局的right right.layout(left.getRight(), 0, left.getRight() + display.getWidth(), display.getHeight()); } @Override public void onDrawerOpened(View drawerView) {} @Override public void onDrawerClosed(View drawerView) { isDrawer=false; } @Override public void onDrawerStateChanged(int newState) {} }); } @Override public void onBackPressed() { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }
drawer.openDrawer(GravityCompat.START);//打開(kāi)滑動(dòng)菜單
drawer.closeDrawer(GravityCompat.START);//關(guān)閉滑動(dòng)菜單
drawer.isDrawerOpen(GravityCompat.START);//判斷滑動(dòng)菜單是否打開(kāi)
關(guān)于“Android中DrawerLayout怎么實(shí)現(xiàn)側(cè)滑菜單效果”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。