刚刚遇到这个问题,也刚刚解决了 参照网上的说法是这样1. /** 2. * Align the first gallery item to the left. 3. * 4. * @param parentView The view containing the gallery widget (we assume the gallery width 5. * is set to match_parent) 6. * @param gallery The gallery we have to change 7. */ 8. private void alignGalleryToLeft(View parentView, Gallery gallery) { 9. int galleryWidth = parentView.getWidth(); 10. 11. // We are taking the item widths and spacing from a dimension resource because: 12. // 1. No way to get spacing at runtime (no accessor in the Gallery class) 13. // 2. There might not yet be any item view created when we are calling this 14. // function 15. int itemWidth = context.getResources() 16. .getDimensionPixelSize(R.dimen.gallery_item_width); 17. int spacing = context.getResources() 18. .getDimensionPixelSize(R.dimen.gallery_spacing); 19. 20. // The offset is how much we will pull the gallery to the left in order to simulate 21. // left alignment of the first item 22. int offset; 23. if (galleryWidth <= itemWidth) { 24. offset = galleryWidth / 2 - itemWidth / 2 - spacing; 25. } else { 26. offset = galleryWidth - itemWidth - 2 * spacing; 27. } 28. offset = 0; 29. 30. // Now update the layout parameters of the gallery in order to set the left margin 31. MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams(); 32. mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin); 33. } 但是试过以后出现了一些问题:其中第一个问题是第9行:int galleryWidth = parentView.getWidth(); 我得到的0,因为我的gallery是横向铺满全屏的,所以我用了int galleryWidth = this.getWindowManager().getDefaultDisplay().getWidth();这种方法来得到屏幕的宽,也就等于gallery的宽; 还有第28行,很明显的错误,offset = 0; 它要等于0,前面不等于白设置了; 我自己还遇到一个问题,就是16和18行出现的“R.dimen”,以前没用过,原来是用来定义尺寸的,就像String是用来定义字符串一个意思 最后,终于搞定了! 但我还有一个问题,怎么让它在点击图片之后,图片不再移动,因为这样做之后,点击的图片不再移到中间,而是移到最左了,继续百度,google……
参照网上的说法是这样1. /**
2. * Align the first gallery item to the left.
3. *
4. * @param parentView The view containing the gallery widget (we assume the gallery width
5. * is set to match_parent)
6. * @param gallery The gallery we have to change
7. */
8. private void alignGalleryToLeft(View parentView, Gallery gallery) {
9. int galleryWidth = parentView.getWidth();
10.
11. // We are taking the item widths and spacing from a dimension resource because:
12. // 1. No way to get spacing at runtime (no accessor in the Gallery class)
13. // 2. There might not yet be any item view created when we are calling this
14. // function
15. int itemWidth = context.getResources()
16. .getDimensionPixelSize(R.dimen.gallery_item_width);
17. int spacing = context.getResources()
18. .getDimensionPixelSize(R.dimen.gallery_spacing);
19.
20. // The offset is how much we will pull the gallery to the left in order to simulate
21. // left alignment of the first item
22. int offset;
23. if (galleryWidth <= itemWidth) {
24. offset = galleryWidth / 2 - itemWidth / 2 - spacing;
25. } else {
26. offset = galleryWidth - itemWidth - 2 * spacing;
27. }
28. offset = 0;
29.
30. // Now update the layout parameters of the gallery in order to set the left margin
31. MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
32. mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
33. }
但是试过以后出现了一些问题:其中第一个问题是第9行:int galleryWidth = parentView.getWidth();
我得到的0,因为我的gallery是横向铺满全屏的,所以我用了int galleryWidth = this.getWindowManager().getDefaultDisplay().getWidth();这种方法来得到屏幕的宽,也就等于gallery的宽;
还有第28行,很明显的错误,offset = 0; 它要等于0,前面不等于白设置了;
我自己还遇到一个问题,就是16和18行出现的“R.dimen”,以前没用过,原来是用来定义尺寸的,就像String是用来定义字符串一个意思
最后,终于搞定了!
但我还有一个问题,怎么让它在点击图片之后,图片不再移动,因为这样做之后,点击的图片不再移到中间,而是移到最左了,继续百度,google……