2015-06-15 26 views
6

Tôi đã khai báo GirdView trong bố cục xml như hình dưới đây.Trong Android, làm thế nào tôi có thể giữ cho verticalSpacing luôn bằng với HorizontalSpacing trong GridView?

<GridView 
    android:id="@+id/movies_gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:drawSelectorOnTop="true" 
    android:columnWidth="92dp" 
    android:numColumns="auto_fit" 
    android:stretchMode="spacingWidthUniform" 
    android:gravity="center_horizontal" /> 

tôi dự định để hiển thị hình ảnh kích thước cố định (hình chữ nhật dọc không vuông) trong GridView. này tôi đã thiết lập các columnWidth-auto_fit để không gian ngang có thể được sử dụng một cách tối ưu bởi GridView. theo kích thước màn hình có sẵn. Tôi đã đặt stretchMode thành spacingWidthUniform để các cột sẽ được đặt ở khoảng cách bằng nhau từ tất cả các cạnh.

Tôi muốn đặt verticalSpacing bằng horizontalSpacing. Tôi không thể làm điều đó trong xml ở đây vì thời gian chạy horizontalSpacing sẽ phụ thuộc vào không gian có sẵn trên màn hình. Ngoài ra, khi định hướng thiết bị được thay đổi, horizontalSpacing sẽ thay đổi.

Câu hỏi của tôi là:

  1. Làm thế nào tôi có thể chắc chắn rằng verticalSpacing sẽ luôn luôn bằng với horizontalSpacing?

  2. Làm cách nào để làm cho GridView bắt đầu bố trí cột đầu tiên từ đầu cộng với đệm và bố cục kết thúc cột cuối cùng đến cuối GridView trừ đệm?

EDIT 1

tôi đã cố gắng @simas giải pháp để sử dụng xem lưới tùy chỉnh đề cập. Đối với điều đó,

  1. Tôi đã sao chép lớp này vào thư mục nguồn của mình.

  2. Bao gồm chế độ xem lưới này trong bố cục xml như được hiển thị bên dưới.

<com.aviras.ashish.popularmoviesapp.widgets.CustomGridView 
    android:id="@+id/movies_gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnWidth="@dimen/grid_item_width" 
    android:drawSelectorOnTop="true" 
    android:gravity="center_horizontal" 
    android:numColumns="auto_fit" 
    android:stretchMode="spacingWidthUniform" 
    tools:listitem="@layout/grid_item_movie_poster" /> 
  1. Đặt adapter để GridView này như hình dưới đây

    
    CustomGridView moviesGridView = (CustomGridView) rootView.findViewById(R.id.movies_gridview); 
    moviesGridView.setAdapter(new MoviesAdapter(rootView.getContext().getApplicationContext(), mMovies)); 
    

Nó vẫn cho thấy khoảng cách khác nhau cho các cột và hàng như được hiển thị bên dưới. enter image description here

EDIT 2

@simas trong câu hỏi thứ hai của 'bắt đầu layouting' Tôi có nghĩa là GridView nên bắt đầu vẽ cột đầu tiên vào lúc bắt đầu của GridView và cột cuối cùng của GridView nên kết thúc ở cuối số GridView. Ngoài ra, các cột và hàng phải có khoảng cách bằng nhau. Hãy cho tôi biết nếu điều đó làm rõ thêm câu hỏi của tôi.

Trả lời

0

Bạn có thể tạo một lớp tùy chỉnh GridView và sửa đổi các chức năng của setHorizontalSpacingsetVerticalSpacing như thế này:

public class CustomGridView extends GridView { 

    public CustomGridView(Context context) { 
     super(context); 
    } 

    public CustomGridView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomGridView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CustomGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public void setHorizontalSpacing(int horizontalSpacing) { 
     super.setHorizontalSpacing(horizontalSpacing); 
     // Call parent class to set the vertical spacing to be equal to the horizontal spacing 
     super.setVerticalSpacing(horizontalSpacing); 
    } 

    @Override 
    public void setVerticalSpacing(int verticalSpacing) { 
     // Prevent the derived class from modifying the vertical spacing 
     /*super.setVerticalSpacing(verticalSpacing);*/ 
    } 

} 

Sau đó, sử dụng lớp tùy chỉnh trong xml như thế này:

Đối với bạn câu hỏi thứ hai: Tôi thực sự không thể hiểu ý bạn là "bắt đầu bố trí".

Ví dụ hình ảnh về kết quả mong muốn của bạn sẽ giúp ích rất nhiều.

+0

Cảm ơn bạn đã trả lời câu trả lời này. Sẽ thử điều này và cập nhật ở đây. –

+0

vui lòng xem câu hỏi đã chỉnh sửa của tôi. –

0

bạn có thể sử dụng chế độ xem văn bản theo yêu cầu của lưới điện và đặt không gian như bạn muốn và cũng có thể đặt nó theo chiều ngang hoặc theo chiều dọc dễ dàng.

+0

Tôi nên đặt chiều cao và chiều rộng thành TextView sao cho cả hai horizontalSpacingverticalSpacing trong GridView đều bằng nhau? –

0
<GridView 
      android:id="@+id/gridView" 
      android:layout_width="fill_parent" 
      android:layout_height="350dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:layout_marginTop="10dp" 
      android:clickable="true" 
      android:columnWidth="80dp" 
      android:drawSelectorOnTop="true" 
      android:focusable="true" 
      android:gravity="center" 
      android:numColumns="auto_fit" 
      android:smoothScrollbar="true" 
      android:splitMotionEvents="true" 
      android:stretchMode="columnWidth" 
      android:verticalSpacing="5dp" /> 




and textview that is : 

<TextView 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="30dp" 
     android:layout_gravity="bottom" 
     android:background="#55d3d3d3" 
     android:gravity="center_vertical|center_horizontal" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:text="asdfasdadasds" 
     android:textColor="#77FFFFFF" /> 
+0

Điều này không hiệu quả đối với tôi. Vấn đề của tôi là giữ cho HorizontalSpacing == verticalSpacing. Giải pháp bạn đã thêm khoảng cách dọc mà cũng có thể được thêm với thuộc tính verticalSpacing. Vấn đề tôi muốn giải quyết là thỏa mãn điều kiện horizontalSpacing == verticalSpacing trong mọi trường hợp trên mọi kích cỡ màn hình. \ –

Các vấn đề liên quan