[英]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.我认为您可以简单地将可见性设置为消失。
简单的方法是在RelativeLayout
或LinearLayout
添加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.