Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

android - Gridview with layout (imageview + imagebutton) for every value

Is it possible to make a gridview that has instead of a grid of pictures, a grid of pictures with a small imagebutton below every one of them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer: Yes. You can have an ImageView and an ImageButton in a GridView.

Long answer:

You will naturally have to create a custom GridView for that purpose.

For example:

Create an XML which will hold the container GridView, say, 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>

And, to define the contents of the GridView, create another XML layout which will hold the ImageView and the ImageButton. Say, 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>

Finally, if you are familiar with the concept of custom ListViews, with a few modifications, you will be able to implement a custom GridView too. If you are not familiar with custom ListViews or GridViews, follow this tutorial to see how to create a custom GridView: http://www.coderzheaven.com/2012/02/29/custom-gridview-in-android-a-simple-example/. Or use this Google Search for more tutorials on the same.

An important point here would be, if you need the ImageButton's to do a function when they are clicked, the onClickListener will need to be setup in the Adapter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...