我试图从PopupWindow
提示FloatingActionButton
因为这是供其他人使用的SDK的一部分,所以我不能依赖于位置或某些属性FloatingActionButton
相反,我必须在FAB附近找到一个合适的位置来显示PopupWindow
我目前有一些代码似乎在Lollipop和更新版本上正常工作。当我在KitKat上运行它时,FAB
和Popup
之间会有一些额外的间距。我设法通过硬编码一些偏移消除了空间,但这是一个非常难看的解决方案(并且取决于elevation
的{{1}}与我的方式相同。已配置它,这也不是可靠的先决条件)
预期外观(适用于棒棒糖):
KitKat上的外观不正确:
首先我用来呈现弹出窗口的代码:
FAB
我应该能够将0传递给最后两个参数并将弹出窗口显示在FAB旁边。它在棒棒糖上运行良好,之后,但在KitKat上,我必须传递一些依赖于海拔的魔法数字。
这是public void onShowPopup(View view) {
View layout = getLayoutInflater().inflate(R.layout.popup_menu, null);
PositionedPopupWindow.withLayout(layout).showRelativeToView(
view,
PositionedPopupWindow.AnchorPoint.values()[spinner.getSelectedItemPosition()],
0, // getResources().getDimensionPixelSize(R.dimen.fab_hmargin),
0 // getResources().getDimensionPixelSize(R.dimen.fab_vmargin)
);
}
的主体showRelativeToView
PositionedPopupWindow
我试图在public void showRelativeToView(View parent, AnchorPoint anchorPoint, int hMargin, int vMargin) {
if(anchorPoint == AnchorPoint.Automatic) {
anchorPoint = computeAutomaticAnchorPoint(parent);
}
// Get the size of the popup
getContentView().measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
Point popupSize = new Point(getContentView().getMeasuredWidth(), getContentView().getMeasuredHeight());
int gravity = 0, x = 0, y = 0;
// Horizontally align the popup edge closest to the screen edge with the button edge
switch(anchorPoint) {
case TopLeft:
case BottomLeft:
gravity |= Gravity.LEFT;
x += hMargin;
break;
case TopRight:
case BottomRight:
x = -popupSize.x;
x -= hMargin;
gravity |= Gravity.RIGHT;
break;
}
// Vertically align the popup edge closest to the screen edge with the button edge
switch(anchorPoint) {
case TopLeft:
case TopRight:
y = -popupSize.y - parent.getMeasuredHeight();
y += vMargin;
gravity |= Gravity.TOP;
break;
case BottomLeft:
case BottomRight:
// align top of popover and bottom of button
gravity |= Gravity.BOTTOM;
y -= vMargin;
break;
}
PopupWindowCompat.showAsDropDown(this, parent, x, y, gravity);
}
方法中将FAB的边距设置为0,正如其他地方所建议的那样,以消除额外的余量,但它似乎没有改变任何东西。
我的问题是,如何在没有硬编码魔法数字(无法可靠地工作)的情况下摆脱onCreate
和FAB
之间的额外间距
答案 0 :(得分:1)
问题似乎出现了,因为KitKat在FloatingActionButton
附近设置额外的余量来计算高程阴影,因为这会改变FAB的大小,我可以将实际按钮大小与期望大小进行比较并计算必要的抵消:
public void onShowPopup(View view) {
View layout = getLayoutInflater().inflate(R.layout.popup_menu, null);
int hMargin = 0;
int vMargin = 0;
// Pre-Lollipop the FAB shadow is factored into the button size, KitKat and on the
// shadow isn't part of the button size. By comparing the actual width to the expected
// width we can compute an offset necessary to position the popup adjacent to the FAB
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
hMargin += (int) (view.getWidth() - Util.dpToPixels(this, 56)) / 2;
vMargin += (int) (view.getHeight() - Util.dpToPixels(this, 56)) / 2;
}
PositionedPopupWindow.withLayout(layout).showRelativeToView(
view,
PositionedPopupWindow.AnchorPoint.Automatic,
hMargin,
vMargin
);
}