首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android应用开发之(Gallery默许居左解决方案)

2012-06-28 
Android应用开发之(Gallery默认居左解决方案)Android 中的Gallery控件默认会将第一项居中显示,在某些场景

Android应用开发之(Gallery默认居左解决方案)

Android 中的Gallery控件默认会将第一项居中显示,在某些场景会影响用户体验,分享一下居左的解决方案:

/**
* Align the first gallery item to the left.
*
* @param parentView The view containing the gallery widget (we assume the gallery width
* is set to match_parent)
* @param gallery The gallery we have to change
*/
private void alignGalleryToLeft(View parentView, Gallery gallery) {
int galleryWidth = parentView.getWidth();

// We are taking the item widths and spacing from a dimension resource because:
// 1. No way to get spacing at runtime (no accessor in the Gallery class)
// 2. There might not yet be any item view created when we are calling this
// function
int itemWidth = context.getResources()
.getDimensionPixelSize(R.dimen.gallery_item_width);
int spacing = context.getResources()
.getDimensionPixelSize(R.dimen.gallery_spacing);

// The offset is how much we will pull the gallery to the left in order to simulate
// left alignment of the first item
int offset;
if (galleryWidth <= itemWidth) {
offset = galleryWidth / 2 - itemWidth / 2 - spacing;
} else {
offset = galleryWidth - itemWidth - 2 * spacing;
}
offset = 0;

// Now update the layout parameters of the gallery in order to set the left margin
MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
}

?

=====

未验证

?

http://androidit.diandian.com/post/b8c29a20-e368-11e0-94a3-782bcb32ff27

热点排行
Bad Request.