【发布时间】:2015-10-14 00:19:17
【问题描述】:
我正在尝试从FloatingActionButton 呈现PopupWindow 因为这是供其他人使用的SDK 的一部分,所以我不能依赖FloatingActionButton 的位置或某些属性相反,我必须在 FAB 附近找到一个合适的位置来显示PopupWindow 我目前有一些代码似乎在 Lollipop 和更新版本上运行良好。当我在 KitKat 上运行它时,FAB 和 Popup 之间有一些额外的间距。我已经设法通过硬编码一些偏移来消除空间,但这是一个非常丑陋的解决方案(并且取决于FAB 的elevation 与我配置它的方式相同,这也不是一个可靠的前提)
预期外观(适用于 Lollipop):
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)
);
}
我应该能够将 0 传递给最后两个参数,并让弹出窗口显示在 FAB 旁边。它在 Lollipop 及更高版本上运行良好,但在 KitKat 上,我必须传入一些取决于海拔的神奇数字。
这里是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);
}
我尝试在我的 onCreate 方法中将 FAB 的边距设置为 0,正如其他地方所建议的那样,以消除额外的边距,但它似乎没有改变任何东西。
我的问题是,如何在没有硬编码幻数的情况下消除 FAB 和 PopupWindow 之间的额外间距(不能可靠地工作)
【问题讨论】:
-
这可能是因为使用兼容库时模拟的投影。我记得在某处读到过……
-
The documentation for CardView 提到了棒棒糖之前的填充变化; the FloatingActionButton documentation 没有,但不幸的是,它可能会遇到类似的问题。
-
我很确定,如果您在 FAB 外部单击,您会发现该按钮实际上大大超出了它的可见区域。我希望我至少能够找到某种方法来确定额外的填充空间是什么。
标签: java android android-4.4-kitkat floating-action-button