Android - 垂直居中画廊元素

Android - Center Gallery elements vertically

我正在使用一个名为 FancyCoverFlow 的库,它扩展了 Gallery。我尝试添加:

android:gravity="center"

在布局中将子元素居中但运气不好。画廊在整个屏幕上都被拉伸了,我把它的背景设为红色以确保。
如何使 Gallery 子元素垂直居中?

我像你一样试过这个库,但没有成功。所以我建议你把它放在一个布局(RelativeLayout,FrameLayout,LinearLayout ...)中,匹配父宽度和高度,然后将 paddingTop 设置为你的 FancyCoverFlow。

库不再维护,但我还是发布了解决方案。
具体要修改的行是方法dispatchDraw(Canvas canvas) in FancyCoverFlowItemWrapper class中的最后一行:

发件人:

canvas.drawBitmap(this.wrappedViewBitmap, (float)((this.getWidth() - childView.getWidth()) / 2), 0, this.paint);

收件人:

canvas.drawBitmap(this.wrappedViewBitmap, (float)((this.getWidth() - childView.getWidth()) / 2), (float)((this.getHeight() - childView.getHeight()) / 2), this.paint);

只需计算中间并使用它,而不是在 top = 0 处绘制视图。
FancyCoverFlowItemWrapper 实例化驻留在 FancyCoverFlowItem class 中,因此通常您会扩展它并覆盖上述方法,但它不是 public,因此您无法访问它。

我最终 复制 或扩展 classes :

  • FancyCoverFlow -> MyFancyCoverFlow
  • FancyCoverFlowAdapter -> MyFancyCoverFlowAdapter
  • FancyCoverFlowItemWrapper -> MyFancyCoverFlowItemWrapper

所以现在在我的 xml 中,我不再使用 FancyCoverFlow,而是 MyFancyCoverFlow。

这是修复错误的分支: https://github.com/Mehdiway/FancyCoverFlow