Android - 翻译 View 后仍保留空白

标签 android android-actionbar android-animation

我正在尝试为我的工具栏(用作 ActionBar)设置动画以在滚动后隐藏自身。我正在使用以下代码向上翻译工具栏 -

toolbar.animate().translationYBy(toolbar.getBottom()*(-1));

但是,在动画之后,之前被工具栏占用的空间变成了白色并保持在那里。

我想让工具栏下面的内容上移,占据工具栏留下的空间。

最佳答案

动画工具栏后出现白色背景的原因是它嵌入到布局中的方式。

有几种方法可以实现这一点:

  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>
    

Full Screen Mode

  1. 保持工具栏在内容之上

    在为 Toolbar 设置动画时,您还必须为 Activity 的布局设置动画以取而代之。实现细节将取决于您展示的内容。对于 RecyclerView,请参阅 this blog

Full Screen Mode

  1. 隐藏ActionBar

    只需请求操作栏 getSupportActionBar().hide() 隐藏,这将重绘窗口。

enter image description here

此外,如果您想要全屏显示 Immersive Activity ,这应该包括那个。

关于Android - 翻译 View 后仍保留空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28841362/

相关文章:

android - 使用 Google Service API 获取最后已知位置

android - NoActionBar 相当于 Sherlock ActionBar

java - 像 Uber 一样透明的操作栏和状态栏

java - Android,如何更改状态更改时的操作栏 Logo 按钮?

android - android中按钮背后的光线效果动画

java - Android 中的无限按钮动画

安卓 'leaking context' 错误

android - 在 Android 上使用 msvcrt。或类似

android - 在android中获取ActionBar TextView(标题)

android - android屏幕坐标如何工作?