簡體   English   中英

如何以編程方式打開和關閉layout_behavior行為?

[英]How to toggle layout_behavior behavior on and off programmatically?

讓我們假裝簡單的簡單案例。 我有一個FloatingActionButton,我在其上添加了layout_behavior。 我需要能夠以編程方式啟用或禁用該行為。 我怎么做? 我最初通過xml添加行為

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/mine"
    app:backgroundTint="@color/colorPrimary"
    app:layout_anchor="@id/default_message"
    app:layout_anchorGravity="end|bottom"
    app:layout_behavior=“mywidget.ScrollingFABBehavior"/>

您可以通過檢索LayoutParams

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams params = 
    (CoordinatorLayout.LayoutParams) fab.getLayoutParams();

那么你可以直接用setBehavior()設置行為:

params.setBehavior(null);

或者獲取您的行為實例並調用一個方法讓它自行禁用(您所做的):

ScrollingFABBehavior behavior =
    (ScrollingFABBehavior) params.getBehavior();
// This is a method you write
behavior.setEnabled(false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM