Set layout_anchor at runtime on FloatingActionButton

冷暖自知 提交于 2019-12-21 07:23:21

问题


I am trying to animate a android.support.design.widget.FloatingActionButton that is pinned to my AppBarLayout. I can set it fine within the layout xml and it shows up fine. However i am doing a Shared Element Transition to this layout and the FAB is showing up before the view is set. I tried to set the visibility to GONE and INVISIBLE but they seem to be disregarded if the layout_anchor is set in the layout xml. Is there anyway around this?

I would like the activity to load with the shared element transition then fade in my FAB. I just can't hide the FAB on load. I could do it without using the layout_anchor but prefer to keep that if possible.


回答1:


If you have a FAB with the the app:layout_anchor attribute, and you want to set the visibility you should use something like this:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
params.setAnchorId(View.NO_ID);
fab.setLayoutParams(params);
fab.setVisibility(View.GONE);

If you want to set theapp:layout_anchor dinamically you can use the same code:

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


来源:https://stackoverflow.com/questions/31596589/set-layout-anchor-at-runtime-on-floatingactionbutton

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