我正在尝试为我的工具栏(用作 ActionBar)设置动画以在滚动后隐藏自身。我正在使用以下代码向上翻译工具栏 -
toolbar.animate().translationYBy(toolbar.getBottom()*(-1));
但是,在动画之后,之前被工具栏占用的空间变成了白色并保持在那里。
我想让工具栏下面的内容上移,占据工具栏留下的空间。
最佳答案
动画工具栏后出现白色背景的原因是它嵌入到布局中的方式。
有几种方法可以实现这一点:
让工具栏覆盖内容
使用
FrameLayout
来容纳工具栏和 Activity 的内容。<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/picture" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/jokic" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_dark" /> </FrameLayout>
保持工具栏在内容之上
在为 Toolbar 设置动画时,您还必须为 Activity 的布局设置动画以取而代之。实现细节将取决于您展示的内容。对于
RecyclerView
,请参阅 this blog
隐藏
ActionBar
只需请求操作栏
getSupportActionBar().hide()
隐藏,这将重绘窗口。
此外,如果您想要全屏显示 Immersive Activity ,这应该包括那个。
关于Android - 翻译 View 后仍保留空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28841362/