使Gallery时设置居左显示

Gallery中的图片默认是居中显示的。可是在非常多情况下我们须要它居左显示,这样做有一个简单方法。就是把Gallery的left设置为负多少,如以下的方法:
Drawable drawable=categoryItem.getCategorys().get(0).getImage();
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
         
MarginLayoutParams mlp=(MarginLayoutParams)gallery.getLayoutParams();
   mlp.setMargins(-(metrics.widthPixels / 2 + drawable.getIntrinsicWidth()), mlp.topMargin,
                    mlp.rightMargin, mlp.bottomMargin);
gallery.setLayoutParams(mlp);

posted @ 2017-04-25 15:31  claireyuancy  阅读(234)  评论(0编辑  收藏  举报