【问题标题】:Programmatically hide / show android support design TabLayout inside AppBarLayout以编程方式在 AppBarLayout 中隐藏/显示 android 支持设计 TabLayout
【发布时间】:2015-12-09 14:03:26
【问题描述】:

我想在我的AppBarLayout 中以编程方式隐藏/显示TabLayout。仅将可见性设置为 VISIBLE 和 GONE 是不够的,因为我想为更改设置动画并在标签后退时用我的内容回收空间,并在标签显示回来后留下空间。

下面是我的布局 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 应该足够了。
  • 无缝运行!谢谢!如果您可以将其发布为答案,我将能够接受。

标签: android android-animation android-tablayout android-support-design android-appbarlayout


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-10-25
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多