使用DrawerLayout完成滑動菜單的示例代碼
用Toolbar編寫自定義導航欄,在AndroidManifest.xml中你要編滑動菜單的界面處加入如下代碼
<activity android:name=".DrawerLayoutActivity" android:theme="@style/NoTitle"></activity>
在values下的styles.xml中加入
<style name="NoTitle" parent="Theme.AppCompat.Light.NoActionBar">//隱藏原有導航欄 //以下兩條均為顏色的設置 <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> </style>
新建兩個布局文件(用于主布局文件的調用)
important.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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/b" /> </LinearLayout>
left.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/colorPrimary" android:padding="40dp"> <de.hdodenhof.circleimageview.CircleImageView//將圖片圓形化的控件(Design Support庫中提供) android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/cat" android:scaleType="centerCrop" android:layout_gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="你的小可愛" android:textSize="20sp" android:layout_gravity="center"/> </LinearLayout>
將上面圓形化圖片控件的庫引入到項目中
在app/build.gradle的dependencies閉包中加入如下內容
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'de.hdodenhof:circleimageview:2.1.0'//這是需要添加的 testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espressocore:3.2.0' }
在res目錄下新建一個menu文件夾,在menu文件夾下新建一個nav_menu.xml文件,這里的圖片是事先準備好的
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:showIn="navigation_view"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_menu_camera" android:title="Home" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow" /> <item android:id="@+id/nav_tools" android:icon="@drawable/ic_menu_manage" android:title="Tools" /> </group> </menu>
主布局文件activity_drawer_layout.xml,我們用到了DrawerLayout 布局,這里面的NavigationView和上面的將圖片圓形化的控件一樣也需要將庫引入到項目中
<?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:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/important"http://引入上面編好的important.xml文件 android:layout_width="match_parent" android:layout_height="match_parent" /> <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" android:fitsSystemWindows="true" app:headerLayout="@layout/left"http://引入上面編好的left.xml文件 app:menu="@menu/nav_menu">//引入上面編好的nav_menu.xml文件 </com.google.android.material.navigation.NavigationView> </androidx.drawerlayout.widget.DrawerLayout>
將上面NavigationView的庫引入到項目中
在app/build.gradle的dependencies閉包中加入如下內容
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.navigation:navigation-fragment:2.3.0'//這里是需要添加的 implementation 'androidx.navigation:navigation-ui:2.3.0'//這里是需要添加的 implementation 'de.hdodenhof:circleimageview:2.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espressocore:3.2.0'
在DrawerLayoutActivity.java中編寫代碼
import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import com.google.android.material.navigation.NavigationView; public class DrawerLayoutActivity extends AppCompatActivity { private DrawerLayout mDrawerlayout; private ActionBar actionBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawer_layout); mDrawerlayout=findViewById(R.id.drawer_layout);//得到DrawerLayout實例 NavigationView navigationView=findViewById(R.id.nav_view);//獲取到NavigationView實例 Toolbar toolbar = findViewById(R.id.toolbar);//找到Toobar控件 setSupportActionBar(toolbar);//用setSupportActionBar方法將導航欄設置為我們自定義的Toolbar actionBar=getSupportActionBar();//用getSupportActionBar()方法實例化導航欄(這里的導航欄就是我們自定義的Toolbar) if(actionBar!=null){ actionBar.setDisplayHomeAsUpEnabled(true);//讓左上角的導航按鈕顯示出來 actionBar.setHomeAsUpIndicator(R.drawable.caidan);//設置導航按鈕圖標(圖片是提前準備好的) } navigationView.setCheckedItem(R.id.nav_home);//調用NavigationView的setCheckedItem方法將Home菜單設置為默認選中 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {//設置菜單選中事件的監(jiān)聽器 @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { mDrawerlayout.closeDrawers();//選中后關閉菜單 return true; } }); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {//加入的導航按鈕的點擊事件 switch (item.getItemId()){ case android.R.id.home: mDrawerlayout.openDrawer(GravityCompat.START);//調用DrawerLayout的openDrawer方法將菜單展示出來 actionBar=null; break; default: } return true; } }
運行結果展示:
總結
到此這篇關于使用DrawerLayout完成滑動菜單的示例代碼的文章就介紹到這了,更多相關DrawerLayout完成滑動菜單內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android實現簡易計步器功能隔天步數清零查看歷史運動紀錄
這篇文章主要介紹了Android實現簡易計步器功能隔天步數清零查看歷史運動紀錄,需要的朋友可以參考下2017-06-06Android GridView 滑動條設置一直顯示狀態(tài)(推薦)
這篇文章主要介紹了Android GridView 滑動條設置一直顯示狀態(tài)的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12