您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Android中DrawerLayout如何實現側滑菜單效果”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Android中DrawerLayout如何實現側滑菜單效果”這篇文章吧。
效果圖:
開始寫代碼
DrawerLayout 是v4包里面的,所以項目里面需要添加v4包,具體怎么添加就不多說了,
NavigationView需要在build.gradle里面添加compile ‘com.android.support:design:25.1.0'
activity_main.xml
<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"> <!-- 右邊視圖 --> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 左邊菜單 --> <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_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout>
然后在res文件夾里面添加一個menu菜單文件夾,已經添加的就不用添加了
上面的代碼里面NavigationView里面有個 app:menu=”@menu/activity_main_drawer”對應如下:
添加一個菜單文件,名字隨意,我的是activity_main_drawer.xml
activity_main_drawer.xml
<?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_camera" android:icon="@drawable/ic_menu_camera" android:title="第一行" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="第二行" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="第三行" /> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="第四行" /> </group> <item android:title="Communicate"> <menu> <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="第五行" /> <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="第六行" /> </menu> </item> </menu>
再添加一個布局
上面的代碼里面app:headerLayout=”@layout/nav_header_main”對應如下:
添加一個布局,名字是nav_header_main.xml
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" app:srcCompat="@android:drawable/sym_def_app_icon" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="哈哈哈" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123456789@qq.com" /> </LinearLayout>
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(); //設置右面的布局位置 根據左面菜單的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);//打開滑動菜單
drawer.closeDrawer(GravityCompat.START);//關閉滑動菜單
drawer.isDrawerOpen(GravityCompat.START);//判斷滑動菜單是否打開
以上是“Android中DrawerLayout如何實現側滑菜單效果”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。