`

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

Android自定义Gallery,实现CoverFlow效果
http://bigcat.easymorse.com/?p=1391
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics