Android编程实现ActionBar的home图标动画切换效果
作者:books1958 时间:2021-11-21 11:34:11
本文实例讲述了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() 方法。
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
希望本文所述对大家Android程序设计有所帮助。
标签:Android,ActionBar,home,动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity 如何获取鼠标停留位置下的物体
2023-09-05 17:17:14
java简单实现复制 粘贴 剪切功能代码分享
2023-10-14 05:08:23
Java实现导出word表格的示例详解
2023-01-02 21:36:48
![](https://img.aspxhome.com/file/2023/1/105961_0s.png)
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
![](https://img.aspxhome.com/file/2023/9/58909_0s.png)
SpringBoot多线程进行异步请求的处理方式
2021-11-10 10:48:30
详解android 人脸检测你一定会遇到的坑
2023-03-10 15:24:34
![](https://img.aspxhome.com/file/2023/1/118951_0s.jpg)
Logger.error打印错误异常的详细堆栈信息
2022-01-06 23:03:04
![](https://img.aspxhome.com/file/2023/3/73033_0s.jpg)
OpenGL绘制三次Bezier曲线
2022-04-23 18:18:14
![](https://img.aspxhome.com/file/2023/3/112283_0s.jpg)
C#使用符号表实现查找算法
2022-08-07 01:16:45
![](https://img.aspxhome.com/file/2023/8/122678_0s.jpg)
对int array进行排序的实例讲解
2021-12-09 06:51:15
![](https://img.aspxhome.com/file/2023/1/68101_0s.png)
在 C# 中使用 Span<T> 和 Memory<T> 编写高性能代码的详细步骤
2022-06-06 05:06:41
![](https://img.aspxhome.com/file/2023/8/96388_0s.png)
介绍java中Pair(配对)
2021-09-03 10:15:39
浅谈Java并发中的内存模型
2022-09-07 17:54:47
![](https://img.aspxhome.com/file/2023/3/62093_0s.png)
Java实现简易Web服务器
2023-11-17 09:10:11
springboot+zookeeper实现分布式锁的示例代码
2022-02-05 08:48:48
![](https://img.aspxhome.com/file/2023/9/85469_0s.jpg)
浅析Disruptor高性能线程消息传递并发框架
2023-02-26 14:09:01
![](https://img.aspxhome.com/file/2023/8/74988_0s.png)
关于springboot加载yml配置文件的no字段自动转义问题
2021-11-02 15:47:48
![](https://img.aspxhome.com/file/2023/0/129250_0s.png)
Java中final与继承操作实例分析
2023-09-14 08:56:02
![](https://img.aspxhome.com/file/2023/4/114044_0s.png)
java高并发的线程中断的几种方式详解
2022-08-25 01:35:09
springboot框架阿里开源低代码工具LowCodeEngine
2022-09-01 09:40:41
![](https://img.aspxhome.com/file/2023/7/131887_0s.png)