Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫切換效果
本文實(shí)例講述了Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫切換效果。分享給大家供大家參考,具體如下:
Material Design中一個(gè)重要特性是側(cè)滑菜單 展開/關(guān)閉 時(shí),ActionBar上的home圖標(biāo)也動(dòng)畫切換。本例要實(shí)現(xiàn)的正是這個(gè)效果,如圖所示:
實(shí)現(xiàn)這個(gè)效果僅需幾步:
1.首先,該頁面的布局是一個(gè)DrawerLayout,代碼如下:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_drawer" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 內(nèi)容布局--> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 側(cè)滑菜單--> <android.support.design.widget.NavigationView android:id="@+id/main_navigation" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/navigation_header" app:menu="@menu/menu_drawer" /> </android.support.v4.widget.DrawerLayout>
2.為程序指定Actionbar箭頭按鈕樣式,即如下代碼中的DrawerArrowStyle
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@android:color/white</item> </style>
然后,將AppTheme應(yīng)用到manifest中application標(biāo)簽下。
3. Activity繼承自AppCompatActivity, 然后在onCreate方法中添加代碼(使用Toolbar與此類似):
ActionBar mActionBar = getSupportActionBar(); if (mActionBar != null) { mActionBar.setDisplayHomeAsUpEnabled(true); mActionBar.setHomeButtonEnabled(true); } //實(shí)現(xiàn)左側(cè)home圖標(biāo)“菜單”樣式與“返回”樣式的動(dòng)畫切換(需要在xml中配置相關(guān)樣式) drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close); drawerLayout.setDrawerListener(drawerToggle);
4.在Activity的onPostCreate中添加如下代碼,并且在其它可能需要刷新的地方調(diào)用drawerToggle.syncState() 方法。
@Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); drawerToggle.syncState(); }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)動(dòng)畫技巧匯總》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義ActionBar實(shí)例
- Android中ActionBar以及menu的代碼設(shè)置樣式
- android中開啟actionbar的兩種方法
- Android ActionBar使用教程
- 靈活使用Android中ActionBar和ViewPager切換頁面
- Android巧用ActionBar實(shí)現(xiàn)tab導(dǎo)航效果
- Android ActionBar搜索功能用法詳解
- Android巧用ActionBar實(shí)現(xiàn)下拉式導(dǎo)航
- Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼
- Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)
- Android編程之ActionBar Tabs用法實(shí)例分析
- Android學(xué)習(xí)筆記之ActionBar Item用法分析
相關(guān)文章
詳解Android中visibility屬性VISIBLE、INVISIBLE、GONE的區(qū)別
在Android開發(fā)中,大部分控件都有visibility這個(gè)屬性,其屬性有3個(gè)分別為“visible ”、“invisible”、“gone”。主要用來設(shè)置控制控件的顯示和隱藏。本文就詳細(xì)的講解一下。2016-12-12Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
這篇文章主要介紹了Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼,是Android程序開發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法
這篇文章主要介紹了Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法,通過ListView控件綁定setOnScrollListener方法簡單實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的功能,需要的朋友可以參考下2016-01-01Android開發(fā)歡迎頁點(diǎn)擊跳過倒計(jì)時(shí)進(jìn)入主頁
沒點(diǎn)擊跳過自然進(jìn)入主頁,點(diǎn)擊跳過之后立即進(jìn)入主頁,這個(gè)功能怎么實(shí)現(xiàn)呢,本文通過實(shí)例代碼給大家介紹Android開發(fā)歡迎頁點(diǎn)擊跳過倒計(jì)時(shí)進(jìn)入主頁,感興趣的朋友一起看看吧2023-12-12Android中多個(gè)ContentProvider的初始化順序詳解
在日常Android開發(fā)中經(jīng)常會(huì)寫一些sdk來供他人或者自己調(diào)用,一般這些sdk都涉及到初始化,下面這篇文章主要給大家介紹了關(guān)于Android中多個(gè)ContentProvider的初始化順序的相關(guān)資料,需要的朋友可以參考下2022-04-04Flutter如何通過一行命令解決多個(gè)pubspec.yaml文件的依賴項(xiàng)問題
這篇文章主要介紹了Flutter如何通過一行命令解決多個(gè)pubspec.yaml文件的依賴項(xiàng)問題,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06Android實(shí)現(xiàn)清除單個(gè)域名的cookie
這篇文章主要介紹了Android實(shí)現(xiàn)清除單個(gè)域名的cookie,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03詳解Android開發(fā)中ContentObserver類的使用
這篇文章主要介紹了詳解Android開發(fā)中ContentObserver類的使用,ContentObserver內(nèi)容觀察者主要用來監(jiān)聽uri的改變請(qǐng)情況,需要的朋友可以參考下2016-04-04