国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現ActionBar的home圖標動畫切換效果

2019-12-12 03:58:21
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現ActionBar的home圖標動畫切換效果。分享給大家供大家參考,具體如下:

Material Design中一個重要特性是側滑菜單 展開/關閉 時,ActionBar上的home圖標也動畫切換。本例要實現的正是這個效果,如圖所示:

實現這個效果僅需幾步:

1.首先,該頁面的布局是一個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">  <!-- 內容布局-->  <FrameLayout    android:id="@+id/main_content"    android:layout_width="match_parent"    android:layout_height="match_parent" />  <!-- 側滑菜單-->  <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應用到manifest中application標簽下。

3. Activity繼承自AppCompatActivity, 然后在onCreate方法中添加代碼(使用Toolbar與此類似):

ActionBar mActionBar = getSupportActionBar();if (mActionBar != null) {  mActionBar.setDisplayHomeAsUpEnabled(true);  mActionBar.setHomeButtonEnabled(true);}//實現左側home圖標“菜單”樣式與“返回”樣式的動畫切換(需要在xml中配置相關樣式)drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);drawerLayout.setDrawerListener(drawerToggle);

4.在Activity的onPostCreate中添加如下代碼,并且在其它可能需要刷新的地方調用drawerToggle.syncState() 方法。

@Overrideprotected void onPostCreate(Bundle savedInstanceState) {    super.onPostCreate(savedInstanceState);    drawerToggle.syncState();}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發動畫技巧匯總》、《Android編程之activity操作技巧總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 无极县| 永春县| 乌海市| 桑日县| 商水县| 松滋市| 芦溪县| 汨罗市| 聂荣县| 壤塘县| 石景山区| 镶黄旗| 阳山县| 若尔盖县| 江孜县| 铁岭市| 临江市| 社会| 贵阳市| 怀宁县| 苍梧县| 黄大仙区| 报价| 夏邑县| 柏乡县| 上杭县| 凭祥市| 富顺县| 青铜峡市| 西藏| 汉源县| 太保市| 灵台县| 土默特左旗| 郴州市| 泰州市| 从江县| 忻州市| 石屏县| 舟山市| 宿州市|