简体   繁体   English

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

[英]Android: How can we hide/show floating action button(fab) in xml layout

I have a Floating Action Button in my Activity.我的活动中有一个浮动操作按钮。 I am able to show/hide the fab button programmatically using:我可以使用以下方法以编程方式显示/隐藏 fab 按钮:

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

But I'm unable to set these properties in xml layout.但我无法在 xml 布局中设置这些属性。 I couldn't find anything to set it hide by default in xml.我找不到任何东西可以在 xml 中默认设置它隐藏。 Here is my xml layout.这是我的 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" />

UPDATE: I dont want to set my fab button visibility to GONE.更新:我不想将我的 fab 按钮可见性设置为 GONE。 I want to set hide() in XML layout.我想在 XML 布局中设置 hide()。 I want to call show() on fab in my activity after a delay.我想在延迟后在我的活动中调用 fab 上的 show() 。

In "FloatingActionButtonLollipop.java" and "FloatingActionButtonEclairMr1.java", show() checks its visibility first then play animation.在“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);
    } 

I think you can simply set visibility to gone.我认为您可以简单地将可见性设置为消失。

简单的方法是在RelativeLayoutLinearLayout添加FloatingActionButton并使用GONE/VISIBLE动作到容器布局。

To have FloatingActionButton show() call animated effect you need to use visibility="invisible" , so the button's view is laid out in the layout.FloatingActionButton show()调用的动画效果,你需要使用visibility="invisible" ,使按键的观点在布局布局

See the implementation of the show method in support library:查看支持库中show方法的实现:

void show(@Nullable final InternalVisibilityChangedListener listener, final boolean fromUser) {
        if (isOrWillBeShown()) {
            // We either are or will soon be visible, skip the call
            return;
        }

        mView.animate().cancel();

        if (shouldAnimateVisibilityChange()) {
            mAnimState = ANIM_STATE_SHOWING;
...

and the method和方法

private boolean shouldAnimateVisibilityChange() {
    return ViewCompat.isLaidOut(mView) && !mView.isInEditMode();
}
  android:visibility="invisible"

I tried this on xml and simply called fab.show() It works with animation in my device.我在 xml 上尝试了这个并简单地调用了fab.show()它在我的设备中使用动画。 (Andoird 5.1.1) (安道尔 5.1.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过xml隐藏FAB(浮动操作按钮)的任何方法? - Any way to hide FAB(floating action button) via xml? 如何使用FAB(浮动操作按钮)隐藏或显示我的编辑文本框? - How do I use a FAB(floating action button) to hide or show my edit text box? 在 recyclerview 单击时隐藏浮动操作按钮 (FAB) - Hide Floating Action Button (FAB) on recyclerview click 我们如何将浮动操作按钮放在任何布局的顶部 - How we can put Floating action button on top of any layout 如何在android上更改浮动操作按钮(FAB)的形状? - How to change the shape of Floating Action Button (FAB) on android? 如何在 Android 的浮动操作按钮 FAB 中保留可绘制的默认颜色 - How to preserve drawable default color in a Floating Action Button FAB in Android Android - 可移动/可拖动的浮动操作按钮 (FAB) - Android - Movable/Draggable Floating Action Button (FAB) 滚动/粘性FAB浮动操作按钮android - Scrolling / sticky FAB floating action button android Android L - 浮动操作按钮(FAB) - Android L - Floating Action Button (FAB) Android:具有ExpandableListView的浮动操作按钮(fab) - Android: Floating Action Button (fab) with ExpandableListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM