2013-02-11 24 views

Trả lời

7

Câu trả lời ngắn: Có. Bạn có thể có một số ImageViewImageButton trong một GridView.

Long trả lời:

Bạn sẽ tự nhiên có để tạo ra một tùy chỉnh GridView cho mục đích đó.

Ví dụ:

Tạo một XML mà sẽ giữ container GridView, nói, grid.xml:

<GridView 
    android:id="@+id/gridFriends" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clipChildren="true" 
    android:columnWidth="100dp" 
    android:fastScrollEnabled="true" 
    android:gravity="center" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" > 
</GridView> 

Và, để xác định các nội dung của GridView, tạo một layout XML mà sẽ giữ ImageViewImageButton. Nói, grid_items.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="5dp" > 

    <FrameLayout 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/imgProfilePicture" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:adjustViewBounds="true" 
      android:scaleType="centerCrop" 
      android:src="@null" /> 

     <ImageButton 
      android:id="@+id/imgbtnDemo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|center" 
      android:background="@null" 
      android:gravity="center" 
      android:src="@drawable/ic_contact_picture" > 
     </ImageButton> 
    </FrameLayout> 

</RelativeLayout> 

Cuối cùng, nếu bạn đã quen thuộc với khái niệm tùy chỉnh ListViews, với một vài thay đổi, bạn sẽ có thể thực hiện một phong tục GridView quá. Nếu bạn không quen với tùy chỉnh ListViews hoặc GridViews, hãy làm theo hướng dẫn này để xem cách tạo một tùy chỉnh GridView: http://www.coderzheaven.com/2012/02/29/custom-gridview-in-android-a-simple-example/. Hoặc sử dụng Google Search này để có thêm hướng dẫn.

Một điểm quan trọng ở đây sẽ là, nếu bạn cần ImageButton's để làm một chức năng khi chúng được nhấn vào, các onClickListener sẽ cần phải được thiết lập trong Adapter.

+1

cảm ơn, trang web này: http://www.coderzheaven.com/2012/02/29/custom-gridview-in-android-a-simple-example/ + của xml bạn đã giúp tôi rất nhiều trong việc thực hiện GridView tùy chỉnh của tôi. Rất tốt câu trả lời –

+0

@rosualin: Bạn được chào đón. Rất vui được giúp đỡ. :-) –

+0

Làm cách nào để tăng bố cục tương đối trong grid_items? Tôi nhận được ngoại lệ này: java.lang.ClassCastException: android.widget.FrameLayout $ LayoutParams không thể truyền sang android.widget.AbsListView $ LayoutParams – Pria

2

GridView hiển thị lưới là Views. Nó có thể hiển thị bất cứ thứ gì mở rộng lớp View. Nó có thể hiển thị một số LinearLayout với một số ImageViewImageButton bên trong.