Android - 翻译视图后仍留有空白区域

时间:2015-03-03 20:32:17

标签: android android-actionbar android-animation

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

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

然而,在动画之后,之前工具栏占用的空间变为白色并保持不变。

我希望工具栏下方的内容向上移动并占据工具栏留下的空间。

1 个答案:

答案 0 :(得分:0)

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

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

  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>
    
  2. Full Screen Mode

    1. 将工具栏放在内容

      之上

      在为工具栏设置动画时,您还必须为活动的布局设置动画以取代它。实施细节取决于您展示的内容。对于RecyclerView,请参阅this blog

    2. Full Screen Mode

      1. 隐藏ActionBar

        只需请求操作栏getSupportActionBar().hide()隐藏,然后重绘窗口即可。

      2. enter image description here

        另外,如果您要进行全屏Immersive活动,这应该涵盖这一点。