android - FloatingActionButton 不会使用 hide() 方法隐藏

标签 android floating-action-button

当用户单击使用 hide() 方法与底部工作表锚定的 fab 按钮时,我试图隐藏 FloatingActionButton,但是晶圆厂不会隐藏。

当前使用支持库版本26.1.0

Activity

public class A_TestActivity extends AppCompatActivity {

    FloatingActionButton fab;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_test_activity);
        fab = findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fab.hide();  // not working
                Toast.makeText(A_TestActivity.this, "hide()", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

XML 布局

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AAAAAA">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <!-- Bottom Sheet -->
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/vBottomSheetRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sample text" />
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <!-- Floating action button -->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:elevation="4dp"
        app:layout_anchor="@id/vBottomSheetRoot"
        app:layout_anchorGravity="top|right|end" />

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

最佳答案

这是由于 app:layout_anchor 属性造成的。您必须在更改可见性之前删除 anchor 。尝试此代码

 CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
 p.setAnchorId(View.NO_ID);
 fab.setLayoutParams(p);
 fab.hide(); 

关于android - FloatingActionButton 不会使用 hide() 方法隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47609195/

相关文章:

java - 点击 FAB 上的 Alpha 背景

android - 我们如何将 float 操作按钮放在任何布局的顶部

android - 显示 CardView 时的 FAB 行为(向上和向下)

android - 在支持库中为 FloatingActionButton 设置边框颜色

android - 错误 : failed linking references. -> QueuedJob

java - Gradle 无法加载 Google playServicesVersion

android - 如何在 ListView (Kotlin) 中每行添加多个图像,例如 3 张图像

android - API 16 至 19 上 AsyncTask 中的 java.lang.ClassNotFoundException

android - 在代理后面添加 Cordova/Phonegap 插件

android - 在Android中将 Canvas 保存为图像