CoordinatorLayout ignores margins for views with anchor

前端 未结 6 2154
春和景丽
春和景丽 2021-02-12 03:49

Given I\'m using a layout like this:



        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-12 04:32

    I was able to get around this issue by using both layout_anchor layout_anchorGravity along with some padding.. The anchor attribute allows you to have a View position itself relative to another view. However, it doesn't exactly work in the same way that RelativeLayout does. More on that to follow.

    layout_anchor specifies which View your desired View should be positioned (i.e., this View should be placed relative to the View specified by the layout_anchor attribute). Then, layout_anchorGravity specifies which side of the relative View the current View will be positioned, using the typical Gravity values (top, bottom, center_horizontal, etc.).

    The issue with using just these two attributes alone is that the center of the View with the anchors will be placed relative to the other View. For example, if you specify a FloatingActionButton to be anchored to the bottom of a TextView, what really ends up happening is that the the center of the FAB is placed along the bottom edge of the TextView.

    To get around this issue, I applied some padding to the FAB, enough such that the top edge of the FAB was touching the bottom edge of the TextView:

    
    

    You might have to increase the padding to get the desired effect. Hope that helps!

提交回复
热议问题