DrawerLayout+NavigationView

  • 2019 年 12 月 19 日
  • 筆記

側滑菜單的實現方式有許多種,之前有寫過一篇SlidingMenu的使用,這次決定記錄下DrawerLayout+NavigationView來實現的過程

  • 這裡說一下DrawerLayout之前是放在android.support.v4.widget包下的,不過AS3.5以後google整合了這些包到androidx中,於是這次我用的包為androidx.drawerlayout.widget.DrawerLayout。
    • DrawerLayout下包裹兩個控制項
      • 第一個是內容控制項
      • 第二個是側滑控制項
      • addDrawerListener用來設置側滑事件
    • NavigationView隨著版本更新這裡也變成了com.google.android.material.navigation.NavigationView
      • headerLayout設置側滑布局的頭部
      • menu設置側滑布局頭部下的條目
      • layout_gravity設置滑出方向,start為從左側划出,end為右側
      • 頭部監聽使用designNavigationView.getHeaderView(0).setOnClickListener即可
      • 條目監聽使用designNavigationView.setNavigationItemSelectedListener即可。

程式碼其實很簡單,不過還是放一下吧,第一次用的話,可以做參考。

<?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"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:id="@+id/drawer_layout"      tools:context=".MainActivity">        <LinearLayout          android:layout_width="match_parent"          android:layout_height="match_parent">        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Hello World!"          app:layout_constraintBottom_toBottomOf="parent"          app:layout_constraintLeft_toLeftOf="parent"          app:layout_constraintRight_toRightOf="parent"          app:layout_constraintTop_toTopOf="parent" />      </LinearLayout>      <LinearLayout          android:layout_width="320dp"          android:layout_height="match_parent"          android:layout_gravity="start"          android:background="@color/colorAccent">       <com.google.android.material.navigation.NavigationView           android:id="@+id/design_navigation_view"           android:layout_width="320dp"           android:layout_height="match_parent"           app:headerLayout="@layout/layout_header"           app:menu="@menu/menu_left"           android:layout_gravity="start"/>      </LinearLayout>    </androidx.drawerlayout.widget.DrawerLayout>

headerLayout布局

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical">        <ImageView          android:layout_width="match_parent"          android:layout_height="match_parent"          android:src="@mipmap/bg_header" />    </LinearLayout>

menu布局

<?xml version="1.0" encoding="utf-8"?>  <menu xmlns:android="http://schemas.android.com/apk/res/android">        <item          android:id="@+id/menu_app_update"          android:title="應用管理"          android:icon="@mipmap/ic_launcher"/>      <item          android:id="@+id/menu_message"          android:title="消息中心"          android:icon="@mipmap/ic_launcher"/>      <item          android:id="@+id/menu_setting"          android:title="設置"          android:icon="@mipmap/ic_launcher"/>    </menu>

程式碼

public class MainActivity extends AppCompatActivity {        @BindView(R.id.drawer_layout)      DrawerLayout mDrawerLayout;      @BindView(R.id.design_navigation_view)      NavigationView designNavigationView;      private View headView;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          ButterKnife.bind(this);  //        mDrawerLayout.addDrawerListener();          headView = designNavigationView.getHeaderView(0);          headView.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View view) {                }          });          designNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {              @Override              public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {                  switch (menuItem.getItemId()){                      case R.id.menu_app_update:                            break;                  }                  return false;              }          });      }  }

打完收工。

Tags: