动态设置FAB锚点重力

时间:2016-06-26 14:37:16

标签: android android-layout floating-action-button

我需要能够从" center"更改FAB的app:layout_anchorGravity="center"。到"底部|开始"从代码。 我找到了这个例子:

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    p.setAnchorId(xxxx);

    fab.setLayoutParams(p);

但是它用于设置锚点而不是锚点引力。

2 个答案:

答案 0 :(得分:9)

CoordinatorLayout.LayoutParams类具有anchorGravity字段。例如:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.anchorGravity = Gravity.BOTTOM | GravityCompat.START;
fab.setLayoutParams(lp);

答案 1 :(得分:0)

与科特林相同

        (rightUglyBox.layoutParams as CoordinatorLayout.LayoutParams)
                       .anchorGravity 
                                     = Gravity.CENTER_VERTICAL or Gravity.LEFT;

*不仅适用于FAB,而且适用于所有布局