最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用DrawerLayout完成滑動菜單的示例代碼

 更新時間:2020年08月14日 09:06:50   作者:Thq LOVE Wsj  
這篇文章主要介紹了使用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>

新建兩個布局文件(用于主布局文件的調(diào)用)
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閉包中加入如下內(nèi)容

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閉包中加入如下內(nèi)容

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);//調(diào)用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);//調(diào)用DrawerLayout的openDrawer方法將菜單展示出來
        actionBar=null;
        break;
      default:
    }
    return true;
  }
 }

運行結(jié)果展示:

在這里插入圖片描述
在這里插入圖片描述

總結(jié)

到此這篇關于使用DrawerLayout完成滑動菜單的示例代碼的文章就介紹到這了,更多相關DrawerLayout完成滑動菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android進階教程之ViewGroup自定義布局

    Android進階教程之ViewGroup自定義布局

    這篇文章主要給大家介紹了關于Android進階教程之ViewGroup自定義布局的相關資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • Android靜態(tài)變量的生命周期 簡單介紹

    Android靜態(tài)變量的生命周期 簡單介紹

    Android靜態(tài)變量的生命周期 簡單介紹,需要的朋友可以參考一下
    2013-06-06
  • Android圖片處理教程之全景查看效果實現(xiàn)

    Android圖片處理教程之全景查看效果實現(xiàn)

    這篇文章主要給大家介紹了關于Android圖片處理教程之全景查看效果實現(xiàn)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-06-06
  • Android第三方微信支付教程

    Android第三方微信支付教程

    這篇文章主要為大家詳細介紹了Android第三方微信支付教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android studio自定義對話框效果

    Android studio自定義對話框效果

    這篇文章主要為大家詳細介紹了Android studio自定義對話框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Android DrawerLayout實現(xiàn)側(cè)拉菜單功能

    Android DrawerLayout實現(xiàn)側(cè)拉菜單功能

    這篇文章主要介紹了Android DrawerLayout實現(xiàn)側(cè)拉菜單功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-06-06
  • Android APK使用Debug簽名重新打包 Eclipse更改默認Debug簽名

    Android APK使用Debug簽名重新打包 Eclipse更改默認Debug簽名

    這篇文章主要介紹了Android APK使用Debug簽名重新打包 Eclipse更改默認Debug簽名等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Android中多行文本末尾添加圖片排版問題的解決方法

    Android中多行文本末尾添加圖片排版問題的解決方法

    這篇文章主要給大家介紹了關于Android中多行文本末尾添加圖片排版問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • Android Notification通知解析

    Android Notification通知解析

    這篇文章主要針對Android Notification通知進行解析,本文主要介紹的是notification通知的使用方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android常用設計模式之原型模式詳解

    Android常用設計模式之原型模式詳解

    這篇文章主要為大家介紹了Android常用設計模式之原型模式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10

最新評論

时尚| 房产| 通河县| 堆龙德庆县| 清原| 佛教| 依安县| 布拖县| 永嘉县| 雅江县| 鄢陵县| 都昌县| 芦山县| 木里| 荣昌县| 吉木萨尔县| 永川市| 柞水县| 娱乐| 福清市| 通江县| 察隅县| 罗平县| 尖扎县| 高淳县| 蕉岭县| 孟津县| 富宁县| 当涂县| 牙克石市| 福安市| 锡林浩特市| 镇江市| 大新县| 玉山县| 辽阳市| 女性| 从江县| 沙洋县| 二连浩特市| 时尚|