2013-02-26 1 views
2

기본적으로 갤러리는 가운데 정렬됩니다. 내가 원하는 동작은 가운데 배치 대신 상위 레이아웃의 첫 번째 항목을 왼쪽 정렬하는 것입니다. 어떻게 할 수 있습니까? 링크를 통해 이동했습니다 : https://stackoverflow.com/questions/4341158/android-align-first-item-in-gallery-to-the-left첫 번째 갤러리 항목을 왼쪽에 정렬하는 방법은 무엇입니까?

그러나 2.3.x 장치에서는 작동하지 않습니다. 어떻게 구현할 수 있습니까?

+0

HTC 2.3.3 장치 및 Kindle Fire에서 작동하지 않는 추가 링크에서 제안 된 답변. – Nitin4Android

답변

0

제공된 링크의 답변 : https://stackoverflow.com/questions/4341158/android-align-first-item-in-gallery-to-the-left 잘 작동합니다. 문제는 내가 프레임을 FrameLayout에 래핑했기 때문에 RelativeLayout으로 변경했을 때 작동하지 않는다는 것이 문제가되지 않는다.

public int getsize(int sizeInDip) { 
    DisplayMetrics metrics = new DisplayMetrics(); 
    metrics = getResources().getDisplayMetrics(); 
    return (int) ((sizeInDip * metrics.density) + 0.5); 
} 

이 방법으로 DPI에서 값을 전달 :

+0

죽은 링크 404 오류 –

2

개인 무효 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()는 방법이다.

희망, 그것은 당신을 위해 작동합니다.

관련 문제