由于无法设置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());
}
}
}
分享到:
相关推荐
Parallax Everywhere 扩展了ImageView和TextView,使其具有视差效果。项目地址:https://github.com/Narfss/ParallaxEverywhere 效果图:如何使用:<FrameLayout android:layout_width="0dp" android:...
<TableRow> <ImageView android:layout_width="wrap_content" android:layout_height="100dp" android:src="@drawable/img" android:gravity="center" android:layout_span="4"/> </TableRow> <TableRow > ...
android:layout_gravity="center_horizontal" android:background="@drawable/budget" android:clickable="false" /> android:textColor="#383838" android:id="@+id/tv_page_budget" android:layout_width...
TextSwitcherHelper 帮助您在Android中使用TextSwitcher 您可以将此Java文件复制到您的项目中,并像 TextSwitcherHelper.make... text.setGravity(Gravity.CENTER); params.height = ViewGroup.LayoutParams.MA
本文实例为大家分享了Android抽奖轮盘的具体代码,供大家参考,具体内容如下 main布局(图片资源请自行寻找,抱歉) ... android:layout_gravity=center> <ImageView android:layout_width=match_parent
shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开 --> android:layout_width="fill_parent" android:layout_...
android:id="@ id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Click Me!" /> </FrameLayout>您的Java...
<Button android:id="@+id/example_row_b_action_1" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginRight="10dp" android:layout_weight="1" ...
android:layout_gravity="bottom|center_horizontal" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:...
android:gravity="center_vertical" android:id="@ id/toolbar" android:background="@color/color_toolbar" > <FrameLayout android:layout_width="match_parent" android:layout_height=...
android:gravity="center" android:textColor="@android:color/white" android:layout_width="match_parent" android:layout_height="match_parent" android:text="ContentView"/> 注意: (1)...
android:gravity="center_vertical"> android:id="@+id/video_list_item_image" android:layout_width="100dp" android:layout_height="70dp" android:src="@mipmap/def_head"/> android:layout_width="0...