由于无法设置FrameLayout的Gravity,所以只能通过重写onLayout事件实现居中的效果了
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
// align child to center
int width = right - left;
int height = bottom - top;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childLeft = (width - childView.getWidth()) / 2;
final int childTop = (height - childView.getHeight()) / 2;
childView.layout(childLeft, childTop,
childLeft + childView.getWidth(),
childTop + childView.getHeight());
}
}
}