我需要能够从" center"更改FAB的app:layout_anchorGravity="center"
。到"底部|开始"从代码。
我找到了这个例子:
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(xxxx);
fab.setLayoutParams(p);
但是它用于设置锚点而不是锚点引力。
答案 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,而且适用于所有布局