2013-06-21 34 views
5

Tôi có một GridLayout trong thời gian chạy (không phải là một GridView) Đây là xml tôiKhoảng cách giữa mục GridLayout

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/grid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:rowCount="4" 
    android:columnCount="3" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    > 

</GridLayout> 

tôi thêm itmes trên runtime. Các mục là một lớp với hình ảnh và văn bản và tất cả mọi thứ hoạt động tốt. Tôi cần có khoảng cách giữa các mục (giống như một GridView)

Tôi làm cách nào để thực hiện việc này?

+0

thử chênh lệch giữa họ – MDMalik

+0

Bạn có thể sử dụng "hình dạng" phong cách cho từng hạng mục của bạn và thêm đệm vào nó trong đó. –

+0

Hình dạng sẽ không hữu ích nếu bạn cần thêm khoảng cách thực tế. Trực quan nó có thể giống nhau, vâng, nhưng về mặt cấu trúc thì không, phải không? Lợi nhuận cũng sẽ không giúp ích gì khi sử dụng trọng lượng (ít nhất là không có trong phiên bản appcompat v24) – milosmns

Trả lời

0

Hãy thử điều này cho một sự khởi đầu

selector_gridview_item_image_blue.xml

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/gridview_item_image_border_blue_phone" android:state_pressed="true"/> 
    <item android:drawable="@drawable/image_border" android:state_focused="true"/> 
    <item android:drawable="@drawable/image_border"/> 

</selector> 

gridview_item_image_border_blue_phone.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

    <solid android:color="@color/blue" /> 

    <stroke 
     android:width="4dp" 
     android:color="@color/blue" /> 

    <padding 
     android:bottom="4dp" 
     android:left="4dp" 
     android:right="4dp" 
     android:top="4dp" /> 

</shape>