Android: visibility:gone 元素很快可见

Android: visibility:gone element is shortly visible

嗨,

我遇到的问题是,当打开 activity 时,可见性设置为 GONE 的 FloatingActionButton 显示非常短,然后又消失了。

该按钮稍后会用到,因此它必须是不可见的。目前,如果有人足够快(主要是在较旧的 devices/android 版本上),他可以在他被允许之前点击按钮。

我的布局与 android 工作室创建的布局几乎相同,只是我将可见性设置为消失:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.fallenritemonk.ludus.game.GameActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_game" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_fields_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_add"
    android:visibility="gone" />

</android.support.design.widget.CoordinatorLayout>

content_game.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fallenritemonk.ludus.game.GameActivity">

<GridView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fieldGrid"
    android:numColumns="9"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp" />

</RelativeLayout>

我不知道问题出在哪里。我还尝试将其设置为 INVISIBLE 而不是 GONE。因为我无法确定用户是否足够快地在他应该能够之前按下按钮,所以阻止他这样做的唯一可能性是删除这个 "flashing up of the FAB"-bug。

如果需要更多信息,请随时询问!!!

感谢您的帮助

尝试禁用锚点 ID,然后设置可见性。应该可以。

尝试以下操作:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams param = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
param.setBehavior(null);
param.setAnchorId(View.NO_ID);
fab.setLayoutParams(param);
fab.setVisibility(View.GONE);

// TO show again
param.setBehavior(new FloatingActionButton.Behavior());
param.setAnchorId(R.id.appbar);
fab.setVisibility(View.VISIBLE);

只需将 fab.hide() 添加到您的 onCreate() 方法(如果使用 Fragment,则添加 onCreateView() )。您可以从 xml 中删除可见性标签,因为不再需要它。如果您想展示您的 FAB,请使用 fab.show();

没有足够的声誉来发表评论,所以我在这里回答 尝试将标记 android.support.design.widget.FloatingActionButton 放在 include 之前。如果没有帮助,请提供 content_game

的内容

问题似乎已经通过简单更新 android 支持设计库得到解决。我现在已经将它从 23.0.1 更新到 23.1.1!