动态设置FAB锚重力 [英] set FAB anchor gravity dynamically

查看:95
本文介绍了动态设置FAB锚重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够从代码中将FAB的app:layout_anchorGravity="center"center更改为bottom|start. 我找到了这个例子:

I need to be able to change the app:layout_anchorGravity="center" of the FAB from center to bottom|start from the code. I've found this example:

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

但这是用于设置锚点,而不是锚点重力.

but it's for setting the anchor and not the anchor gravity.

推荐答案

CoordinatorLayout.LayoutParams类具有相应的anchorGravity字段.例如:

The CoordinatorLayout.LayoutParams class has the anchorGravity field for that. For example:

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

这篇关于动态设置FAB锚重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆