개인 무효 alignGalleryToLeft (갤러리 갤러리) {
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int galleryWidth = manager.getDefaultDisplay().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 = getsize(105);
int spacing = getsize(10);
// The offset is how much we will pull the gallery to the left in order
// to simulate left alignment of the first item
final int offset;
if (galleryWidth <= itemWidth) {
offset = galleryWidth/2 - itemWidth/2 - spacing;
} else {
offset = galleryWidth - itemWidth - 2 * spacing;
}
// 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);
}
의 getSize()는 방법이다.
희망, 그것은 당신을 위해 작동합니다.
HTC 2.3.3 장치 및 Kindle Fire에서 작동하지 않는 추가 링크에서 제안 된 답변. – Nitin4Android