set FAB anchor gravity dynamically

一世执手 提交于 2019-12-23 09:06:10

问题


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.


回答1:


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);



回答2:


Same thing in Kotlin

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

*Works on all layout not just FAB



来源:https://stackoverflow.com/questions/38039574/set-fab-anchor-gravity-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!