以编程方式在AppBarLayout中隐藏/显示Android支持设计TabLayout

12

我想通过编程的方式隐藏/显示我的AppBarLayout中的TabLayout。简单地将可见性设置为VISIBLEGONE是不够的,因为我想要在选项卡退回并腾出空间时使用动画效果,并在选项卡重新显示时恢复该空间。

以下是我布局XML文件的相关部分 -

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabMode="scrollable"
            android:layout_marginStart="42dp"
            android:layout_marginLeft="42dp"
            style="@style/MainTabLayout">

        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

要实现动画效果,只需将android:animateLayoutChanges="true"添加到父AppBarLayout中即可。话虽如此,View.GONE应该已经足够了。 - natario
那个完美无缝地运行了!谢谢!如果你能把它发布为答案,我就可以接受它。 - jaibatrik
1个回答

20

和任何 ViewGroup 子类一样,AppBarLayout 允许在添加/删除子视图时进行自动动画。您只需在布局文件中添加 android:animateLayoutChanges="true"(默认为 false)即可。

至于当视图消失时回收空间内容,您只需使用 setVisibility(View.GONE) 而不是 setVisibility(View.INVISIBLE),因为后者会保留不可见视图的空间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接