DrawerLayout結(jié)合Tollbar實(shí)現(xiàn)菜單側(cè)滑效果
本文實(shí)例為大家分享了DrawerLayout結(jié)合Tollbar實(shí)現(xiàn)菜單側(cè)滑的具體代碼,供大家參考,具體內(nèi)容如下
DrawerLayout(抽屜布局):谷歌官方的控件,可以簡單的實(shí)現(xiàn)側(cè)滑菜單;
此Demo主要是DrawerLayout結(jié)合Toolbar實(shí)現(xiàn)側(cè)滑左上角返回鍵實(shí)現(xiàn)動畫效果,點(diǎn)擊左上角返回鍵實(shí)現(xiàn)動畫效果并且滑出滑入側(cè)滑菜單;
xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="www.dld.com.drawerlayoutdemo.MainActivity" android:orientation="vertical"> <!--app:theme="@style/DrawerArrowStyle"設(shè)置旋轉(zhuǎn)樣式(當(dāng)DrawerLayout滑出返回鍵有一個動畫)--> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/toolbar" android:background="@android:color/holo_blue_dark" app:theme="@style/DrawerArrowStyle"/> <android.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <!--主布局--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e5e5e5"> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:text="主頁面" android:textSize="35sp"/> </LinearLayout> <!-- 側(cè)滑菜單 android:layout_gravity="start"從左邊滑出 android:layout_gravity="end"從右邊滑出 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e5e5e5" android:layout_gravity="start" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="側(cè)滑菜單" android:gravity="center" android:textSize="35sp"/> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>
布局文件非常簡單,就是一個線性布局,上面是toolbar,下面是DrawerLayout,抽屜布局里面放兩個容器布局,上面的是主頁面,下面的是菜單頁面;
想要實(shí)現(xiàn)左上角返回按鈕的動畫必須給toolbar設(shè)置樣式(app:theme="@style/DrawerArrowStyle"):
<!-- 左邊的側(cè)滑箭頭指示 是否翻轉(zhuǎn),顏色--> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@android:color/white</item> </style>
接下來設(shè)置Toolbar和DrawerLayout:
toolbar = (Toolbar) findViewById(R.id.toolbar); mDrawerLayout= (DrawerLayout) findViewById(R.id.drawerLayout); /***************************************Toolbar設(shè)置****************************************/ //把布局中的Toolbar當(dāng)作ActionBar setSupportActionBar(toolbar); //設(shè)置標(biāo)題 getSupportActionBar().setTitle("頤眾商城"); //設(shè)置返回鍵 getSupportActionBar().setDisplayHomeAsUpEnabled(true); /**********************************DrawerLayout設(shè)置****************************************/ //第一步:創(chuàng)建返回鍵,并實(shí)現(xiàn)打開關(guān)/閉監(jiān)聽 ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, 0, 0) { @Override //打開Drawer public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView);//開關(guān)狀態(tài)改為opened } @Override //關(guān)閉Drawer public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView);//開關(guān)狀態(tài)改為closed } }; //第二步:該方法會自動和actionBar關(guān)聯(lián), 將開關(guān)的圖片顯示在了action上,如果不設(shè)置,也可以有抽屜的效果,不過是默認(rèn)的圖標(biāo) mDrawerToggle.syncState(); //第三步:設(shè)置抽屜滑出來,和滑進(jìn)去的監(jiān)聽 mDrawerLayout.setDrawerListener(mDrawerToggle);
點(diǎn)擊打開鏈接免費(fèi)下載源碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決android 項(xiàng)目jar包沖突的問題
這篇文章主要介紹了完美解決android 項(xiàng)目jar包沖突的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android如何優(yōu)雅的處理重復(fù)點(diǎn)擊
這篇文章主要介紹了Android如何優(yōu)雅的處理重復(fù)點(diǎn)擊,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03android 復(fù)制 粘貼 剪切功能應(yīng)用
網(wǎng)上有很多android 復(fù)制 粘貼 剪切功能的文章,只是放到自己的程序中不知道如何處理,現(xiàn)在尋得一可行方法,需要的朋友可以參考下2012-11-11Android 動態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼
在上次android開發(fā)的項(xiàng)目中遇到個這樣的需求,要動態(tài)改變seekbar進(jìn)度條顏色與滑塊顏色的需求,實(shí)現(xiàn)代碼也算比較簡單,對實(shí)現(xiàn)過程感興趣的朋友可以通過本文學(xué)習(xí)下2016-11-11Android自定義View實(shí)現(xiàn)多圖片選擇控件
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)多圖片選擇控件,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-08-08android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Android組件化工具ARouter使用方法詳細(xì)分析
這篇文章主要介紹了Android組件化工具ARouter使用方法,組件化項(xiàng)目存在各個模塊之間耦合,通信麻煩的問題,為了解決這個問題,阿里巴巴的開發(fā)者就搞出了Arouter這個框架,以解決上述問題2022-10-10android studio 4.0 新建類沒有修飾符的方法
這篇文章主要介紹了android studio 4.0 新建類沒有修飾符的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10