简体   繁体   中英

How to make a 4x4 grid of imagebuttons in android?

I've spent a few weeks looking around for how to do this, and have come across things like this and this , but it seems a couple years old/ I'm having trouble understanding how to apply it to what I need.

All I want is to have my imagebuttons in a 4x4 grid on the screen and each button to start a similar activity but with different data. (its a flash-card type of thing, but each card will have different text). It seems like this should be really simple, but with this being my first android app, and my first exposure to xml and java directly, I feel like I'm getting lost.

What is a good way to implement this? I'm not particularly attached to doing a gridView, and am open to anything that works.

Kinda Like this:

在此处输入图片说明

You can implement it using TableLayout. Example below. Obviously you can change the buttons to ImageButtons.

      <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*">
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <Button
                android:id="@+id/button2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/button3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <Button
                android:id="@+id/button4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </TableRow>
     </TableLayout>

You can use a GridView. The following Android docs link contains examples and detailed info of how to use it.

Android docs: https://developer.android.com/guide/topics/ui/layout/gridview.html

https://developer.android.com/reference/android/widget/GridView.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM