歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android設置Gallery 不居中或左靠齊注意事項

Android設置Gallery 不居中或左靠齊注意事項

日期:2017/3/1 10:07:11   编辑:Linux編程

本來看這樣的布局本以為它會居左,但其實不然。仔細看看它內部實現代碼可以看出。它默認是居中顯示第一個。所以造成左邊空出一大截很難看,解決辦法如下:

<Gallery
Android:id="@+id/gallery_go_userface"
android:layout_marginTop="12dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:spacing="12dp"
android:gravity="left"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

1.第一種方法 :photo_gallery.measure(0, 0);
photo_gallery.scrollBy(photo_gallery.getMeasuredWidth()/2+140, 0); 當然這種方法弊端就是需要調整坐標很麻煩。

2.你仔細回想一下適配器裝載listview數據的時候發現有個setSelection(position)這個方法,同理Gallery也是需要裝載數據進去可想也應該有選擇位置的方法。找找果然如猜想。於是很簡單的填滿屏幕方法就是 photo_gallery.setSelection(photo_gallery.getCount()/2);

3.就是重新Gallery視圖,當然這種不可取也是和第一方法原理差不多,要調整坐標。這個方法介紹網上有很多這裡就不介紹了哈。

希望這裡的介紹對你有作用。

Copyright © Linux教程網 All Rights Reserved