【问题标题】:How to remove layout anchor_property of fab in programmatically?如何以编程方式删除fab的布局anchor_property?
【发布时间】:2016-06-28 05:04:21
【问题描述】:

我在布局 xml 中的 fab 上添加了 layout_anchor 属性。单击按钮后,fab 应位于屏幕末尾。为了实现这一点,我正在考虑添加重力属性。但是我如何以编程方式删除 layout_anchor 属性。

layout.xml

   <com.samsoft.sam.Fab
        android:id="@+id/fab"
        app:layout_anchor="@id/asd"
        app:layout_anchorGravity="top|end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

上面是我的 fab 代码,它的位置很完美,现在我需要用 android:gravity="top|end" 以编程方式替换布局锚属性。请帮帮我。

【问题讨论】:

  • 您在 Java 代码中执行此操作。我们能看到吗?

标签: android floating-action-button


【解决方案1】:

我假设您的意思是您希望 FloatingActionButtonlayout_gravity 最终成为 top|end

您可以通过获取FloatingActionButtonCoordinatorLayout.LayoutParams,清除锚的ID,根据需要设置gravity 字段,然后将LayoutParams 设置回FloatingActionButton 来实现。

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.setAnchorId(View.NO_ID);
lp.gravity = Gravity.TOP | GravityCompat.END;
fab.setLayoutParams(lp);

【讨论】:

    【解决方案2】:
    ((CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams()).setAnchorId(View.NO_ID);
    

    这是您要查找的代码。快乐编码:)

    【讨论】:

      猜你喜欢
      • 2012-12-25
      • 1970-01-01
      • 2016-11-26
      • 2016-04-05
      • 2021-11-02
      • 2021-11-07
      • 1970-01-01
      • 2013-06-06
      • 2022-08-18
      相关资源
      最近更新 更多