代码之家  ›  专栏  ›  技术社区  ›  KittoKatto

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

  •  2
  • KittoKatto  · 技术社区  · 8 年前

    我试图隐藏 FloatingActionButton 当用户单击 fab 按钮固定在带有 hide() 方法,但晶圆厂不会隐藏。

    当前使用支持库版本26.1.0

    活动

    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>
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Munir    8 年前

    这是由于 app:layout_anchor 属性在更改可见性之前,必须移除锚点。尝试此代码

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