Android開(kāi)發(fā)實(shí)現(xiàn)抽屜菜單
本文實(shí)例為大家分享了Android開(kāi)發(fā)實(shí)現(xiàn)抽屜菜單的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果

點(diǎn)擊菜單圖表即可進(jìn)入抽屜

代碼實(shí)現(xiàn)
1、打開(kāi)app/build.gradle文件,在dependencies閉包中添加如下內(nèi)容:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:24.2.1'
compile 'de.hdodenhof:circleimageview:2.1.0'
}
2、進(jìn)入想要添加抽屜的界面的layout布局
添加DrawerLayout控件
首先DrawerLayout是一個(gè)布局,在布局中允許放入兩個(gè)直接子控件,第一個(gè)子控件是主屏幕中的內(nèi)容,第二個(gè)空間是滑動(dòng)菜單中顯示的內(nèi)容
原本的界面所有布局內(nèi)容就放在第一個(gè)子控件中
<?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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bk_1"
tools:context="com.luckyxmobile.graphserviceping.MainActivity">
<!-- 內(nèi)容區(qū) -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/setting"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginLeft="8dp"
android:background="@drawable/ic_baseline_menu1"
/>
<!-- android:background="@drawable/ic_baseline_menu_24"-->
<!--原圖標(biāo)寬高 40 52-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="180dp"
android:id="@+id/graphServicePing"
android:gravity="center"
android:text="Graph Service Ping"
android:textColor="#26C6DA"
android:textSize="36dp"/>
<LinearLayout
android:layout_marginTop="32dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/bloder"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:paddingHorizontal="4dp">
<!-- android:paddingHorizontal="16dp"-->
<!-- android:layout_height="wrap_content"-->
<Button
android:minHeight="50dp"
android:id="@+id/btn_input"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:background="@null"
/>
<Button
android:id="@+id/btn_ping"
android:background="@null"
android:layout_weight="4"
android:text="Ping!"
android:textColor="#262626"
android:textSize="25sp"
android:layout_width="0dp"
android:layout_height="80dp"
/>
<!-- android:layout_weight="2"-->
<!-- android:layout_width="50dp"-->
<!-- android:layout_height="50dp"-->
</LinearLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:background="@mipmap/bk_1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
android:layout_gravity="start"這一句很重要,一定要加上
3.NavigationView用來(lái)優(yōu)化滑動(dòng)菜單頁(yè)面的
menu用來(lái)在NavigationView中顯示具體的菜單項(xiàng),headerLayout則用來(lái)在NavigationView中顯示頭布局(這里我只用到了menu,所以我只寫(xiě)menu)
在res下如果沒(méi)有menu目錄,可以新建一個(gè)menu文件夾,然后右鍵menu->new_menu resource file
menu代碼:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_setting"
android:icon="@drawable/ic_launcher_setting_foreground"
android:title="設(shè)置">
</item>
</menu>
可以添加多個(gè)item,不要忘了引用menu
app:menu="@menu/nav_menu"
4.設(shè)置主界面菜單圖表的點(diǎn)擊事件
跟intent不同
setting.setOnClickListener(new View.OnClickListener() { //設(shè)置點(diǎn)擊事件
@Override
public void onClick(View v) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
});
5、設(shè)置抽屜菜單item點(diǎn)擊事件
DrawerLayout mDrawerLayout;
mDrawerLayout=findViewById(R.id.drawerLayout);
NavigationView navView=(NavigationView)findViewById(R.id.nav_view);
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.nav_setting:
startActivity(new Intent(MainActivity.this, Setting.class));
break;
}
mDrawerLayout.closeDrawers();
return false;
}
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android仿騰訊QQ實(shí)現(xiàn)滑動(dòng)刪除 附源碼下載
仿騰訊QQ滑動(dòng)刪除操作,這篇文章主要為大家詳細(xì)介紹了ListView滑動(dòng)刪除的具體操作方法,感興趣的小伙伴們可以參考一下2016-07-07
android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要介紹了android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android編程之語(yǔ)音識(shí)別實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程語(yǔ)音識(shí)別實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android語(yǔ)音識(shí)別的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)類(lèi)似qq微信消息懸浮窗通知功能
這篇文章主要介紹了Android實(shí)現(xiàn)類(lèi)似qq微信消息懸浮窗通知,需要的朋友可以參考下2018-02-02
Android Studio 3.0 新功能全面解析和舊項(xiàng)目適配問(wèn)題
Android Studio是Android的官方IDE。接下來(lái)通過(guò)本文給大家分享Android Studio 3.0 新功能全面解析和舊項(xiàng)目適配問(wèn)題,需要的朋友可以參考下2017-11-11
Android SharedPreferences存取操作以及封裝詳解
SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類(lèi),用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出2021-11-11
android實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識(shí)別
這篇文章主要介紹了android實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能,帶多音字識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽(tīng)事件的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽(tīng)事件的方法,結(jié)合實(shí)例形式分析了Android事件監(jiān)聽(tīng)實(shí)現(xiàn)音量控制的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Flutter學(xué)習(xí)之創(chuàng)建一個(gè)內(nèi)嵌的navigation詳解
我們?cè)趂lutter中可以使用Navigator.push或者Navigator.pushNamed方法來(lái)向Navigator中添加不同的頁(yè)面,從而達(dá)到頁(yè)面調(diào)整的目的。本文就來(lái)聊聊如何創(chuàng)建一個(gè)內(nèi)嵌的navigation吧2023-03-03

