Android:我们如何在 xml 布局中隐藏/显示 float 操作按钮(fab)

标签 android material-design floating-action-button

我的 Activity 中有一个 float 操作按钮。我可以使用以下代码以编程方式显示/隐藏 fab 按钮:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.show()
fab.hide()

但我无法在 xml 布局中设置这些属性。我找不到任何东西来设置它在 xml 中默认隐藏。这是我的 xml 布局。

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:src="@drawable/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        app:layout_anchor="@id/list_view"
        app:layout_anchorGravity="bottom|right|end" />

更新:我不想将我的 fab 按钮可见性设置为 GONE。我想在 XML 布局中设置 hide()。我想在延迟后在我的 Activity 中调用 fab 上的 show() 。

最佳答案

在“FloatingActionButtonLollipop.java”和“FloatingActionButtonEclairMr1.java”中, show() 首先检查其可见性,然后播放动画。

if (mView.getVisibility() != View.VISIBLE || mIsHiding) {
        // If the view is not visible, or is visible and currently being hidden, run
        // the show animation
        mView.clearAnimation();
        mView.setVisibility(View.VISIBLE);
        Animation anim = android.view.animation.AnimationUtils.loadAnimation(
                mView.getContext(), R.anim.design_fab_in);
        anim.setDuration(SHOW_HIDE_ANIM_DURATION);
        anim.setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR);
        anim.setAnimationListener(new AnimationListenerAdapter() {
            @Override
            public void onAnimationEnd(Animation animation) {
                if (listener != null) {
                    listener.onShown();
                }
            }
        });
        mView.startAnimation(anim);
    } 

我认为您可以简单地将可见性设置为消失。

关于Android:我们如何在 xml 布局中隐藏/显示 float 操作按钮(fab),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34350521/

相关文章:

android - ImageButton 提升问题

ListView 中的 Android 上下文菜单

android - 如何将新 Material BottomAppBar 实现为 BottomNavigationView

android - FloatingActionButton 的白色边框

android - 修复 ScrollView 上的一个元素

android - Firebase 存储的上传/下载速度非常慢

angular - 将路由添加到 Angular2 的 MdDialog

java - 如何在没有 Maven 的情况下将 GWT 和 Material Design Library 结合起来?

android - 如何禁用 RecyclerView 项目单击

android - FloatingActionButton 始终可见