`
zhouxiaoli521
  • 浏览: 554305 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics