欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)實(shí)現(xiàn)抽屜菜單

 更新時(shí)間:2021年11月14日 15:48:35   作者:開心星人  
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)抽屜菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

實(shí)現(xiàn)效果

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

代碼實(shí)現(xiàn)

1、打開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,所以我只寫menu)

在res下如果沒有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 Zygote啟動(dòng)構(gòu)造流程及進(jìn)程創(chuàng)建詳解

    Android Zygote啟動(dòng)構(gòu)造流程及進(jìn)程創(chuàng)建詳解

    這篇文章主要為大家介紹了Android Zygote啟動(dòng)構(gòu)造流程及進(jìn)程創(chuàng)建詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 圖解 Kotlin SharedFlow 緩存系統(tǒng)及示例詳解

    圖解 Kotlin SharedFlow 緩存系統(tǒng)及示例詳解

    這篇文章主要為大家介紹了圖解 Kotlin SharedFlow 緩存系統(tǒng)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng)

    ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng)

    這篇文章主要為大家詳細(xì)介紹了ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài)

    Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài)

    這篇文章主要為大家詳細(xì)介紹了Android仿QQ微信實(shí)時(shí)監(jiān)測(cè)網(wǎng)絡(luò)狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android Fragment監(jiān)聽返回鍵的一種合理方式

    Android Fragment監(jiān)聽返回鍵的一種合理方式

    這篇文章主要給大家介紹了關(guān)于Android Fragment監(jiān)聽返回鍵的一種合理方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • android檢查手機(jī)和無(wú)線是否連接的方法

    android檢查手機(jī)和無(wú)線是否連接的方法

    這篇文章主要介紹了android檢查手機(jī)和無(wú)線是否連接的方法,以兩種不同的方法實(shí)現(xiàn)了該功能,是Android程序開發(fā)中非常常見的實(shí)用技巧,需要的朋友可以參考下
    2014-10-10
  • Flutter SystemChrome使用方法詳解

    Flutter SystemChrome使用方法詳解

    這篇文章主要為大家介紹了Flutter SystemChrome使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 詳解Android 折疊屏適配攻略

    詳解Android 折疊屏適配攻略

    這篇文章主要介紹了Android 折疊屏適配攻略,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Android開發(fā)教程之調(diào)用攝像頭功能的方法詳解

    Android開發(fā)教程之調(diào)用攝像頭功能的方法詳解

    這篇文章主要介紹了Android調(diào)用攝像頭功能的方法,詳細(xì)分析了Android調(diào)用攝像頭功能的權(quán)限設(shè)置、功能代碼與實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2016-06-06
  • Android如何在Gradle中更改APK文件名詳解

    Android如何在Gradle中更改APK文件名詳解

    這篇文章主要介紹了Android如何在Gradle中更改APK文件名的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10

最新評(píng)論