Có thể tạo ra một GridView có thay vì một mạng lưới hình ảnh, một mạng lưới hình ảnh với một nút nhỏ bên dưới mỗi một trong số chúng?Gridview với bố trí (imageview + imagebutton) cho mọi giá trị
Trả lời
Câu trả lời ngắn: Có. Bạn có thể có một số ImageView
và ImageButton
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ữ ImageView
và ImageButton
. 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
.
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ố ImageView
và ImageButton
bên trong.
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 –
@rosualin: Bạn được chào đón. Rất vui được giúp đỡ. :-) –
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