Android 中的Gallery控件默认会将第一项居中显示,在某些场景会影响用户体验,分享一下居左的解决方案: /** ? ===== 未验证 ? http://androidit.diandian.com/post/b8c29a20-e368-11e0-94a3-782bcb32ff27
* 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);
}
详细解决方案
Android应用开发之(Gallery默许居左解决方案)
热度:30 发布时间:2016-05-01 19:00:58.0
相关解决方案
- xml/xslt 变量自增。解决方案
- DB2 调用java自定义方法报错,请大牛帮忙~解决方案
- 关于request.getParameterValues()解决方案
- 一个JSP页面,在NETBEANS里报错!解决方案
- WEB环境javamail取不到pop3邮件,解决方案
- 急来者有分,晚了就没了!解决方案
- disable在IE8中可以接收焦点了?解决方案
- 求网页采集器的正则表达式。解决方案
- 关于FckEdit。解决方案
- 在Struts2 如果有多个拦截器栈顺序写错会有什么不同?解决方案
- servlet+jquery1.4.2(ajax error?)解决方案
- 关于el 《c:if》。解决方案
- jsp、taglib、velocity、freemarker等都过时了吗?解决方案
- 有开发过CA认证的朋友,帮个忙!解决方案
- com.microsoft.sqlserver.jdbc.SQLServerException: 列名 n_id 无效。解决方案
- java 缓冲区读写操作,求解?解决方案
- EXTJS中treepanel树内部机制?解决方案
- 一个数学题,求个算法。解决方案
- java 大文件上传?解决方案
- 从JSP中得到访问者的真实IP(很急)!解决方案
- 高分求解!解决方案
- java可以跟PHP对接吗?解决方案
- 有什么办法在JSP页面在不使用<%%>情况下 使用数学函数Math.floor()解决方案
- 在JSP页面调用JavaBean,出错!初学!求指导!解决方案
- 简单的更改记录的语句?解决方案
- JS方法内取的值是false,给方法传递的值却是正确的,请高手指教。解决方案
- 求教各位大神,初学JSP看什么书比较好呢?新一点的。解决方案
- java调用so,解决方案
- MyEclipse怎样运行applet程序?解决方案
- JPA到底是神马?解决方案