How android gallery setting, so the picture is displayed in the left position. Instead of in the middle
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Landlord I have encountered this problem, you solve it
Reply:
Just encountered this problem has just solved
Online reference argument is so
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.}
However, there are some problems after tried:
The first problem is line 9: int galleryWidth = parentView.getWidth ();
I get zero, because my gallery is covered laterally fullscreen, so I used the int galleryWidth = this.getWindowManager () getDefaultDisplay () getWidth ();.. This method to get a wide screen is the same gallery wide;
There are 28 lines, it is obvious errors, offset = 0; it is equal to 0, the front is not equal to the white set;
I also encountered a problem that "R.dimen" 16 and 18 lines appear, never used, is used to define the size of the original, like a string String is used to define the meaning of
Finally, finally get it!
But I have another question, how to make it after clicking on the picture, the picture does not move, because after doing so, click on the picture is no longer to the middle, but moved to the left, and continue to Baidu, google ......
Reply:
With demand! ! !

Reply:
In addition to the left, the first interface, how to set the number of pictures of it?
No comments:
Post a Comment