如果你有一个这样的按钮,在一个“接缝”分开两个视图
(例如,使用 RelativeLayout) ,底部为负
布局边距与边框重叠时,你会注意到一个问题:
FAB 的大小实际上是非常 与众不同对棒棒糖。
您可以在 AS 的可视化布局编辑器中看到这一点
当您在 API 之间切换时——当您切换到
额外大小的原因似乎是
阴影将视图的大小扩展到每个方向,因此你必须
当你调整 FAB 的利润时,如果它接近于其他利润,就要考虑到这一点
东西。
这里有一个方法可以移除或者
如果填充物太多,就改变填充物:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) myFab.getLayoutParams();
p.setMargins(0, 0, 0, 0); // get rid of margins since shadow area is now the margin
myFab.setLayoutParams(p);
}
Also, I was going to programmatically place the FAB on the "seam"
between two areas in a RelativeLayout by grabbing the FAB's height,
dividing by two, and using that as the margin offset. But
myFab.getHeight() returned zero, even after the view was inflated, it
seemed. Instead I used a ViewTreeObserver to get the height only
after it's laid out and then set the position. See this tip
here. It looked like this:
ViewTreeObserver viewTreeObserver = closeButton.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
closeButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
closeButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
// not sure the above is equivalent, but that's beside the point for this example...
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) closeButton.getLayoutParams();
params.setMargins(0, 0, 16, -closeButton.getHeight() / 2); // (int left, int top, int right, int bottom)
closeButton.setLayoutParams(params);
}
});
}