如何在 android 中制作 4x4 的图像按钮网格?
How to make a 4x4 grid of imagebuttons in android?
我花了几个星期四处寻找如何做到这一点,并遇到了 this and this 之类的事情,但它似乎已经有几年了/我无法理解如何应用它到我需要的。
我想要的只是让我的图像按钮位于屏幕上的 4x4 网格中,并且每个按钮都启动一个类似的 activity 但具有不同的数据。 (这是一种闪卡类型的东西,但每张卡片都有不同的文字)。看起来这应该很简单,但这是我的第一个 android 应用程序,也是我第一次直接接触 xml 和 java,我觉得我迷路了。
实现这个的好方法是什么?我不是特别喜欢做一个 gridView,并且对任何有用的东西都持开放态度。
有点像这样:
您可以使用 TableLayout 来实现它。下面的例子。显然,您可以将按钮更改为 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>
您可以使用 GridView。以下 Android 文档 link 包含有关如何使用它的示例和详细信息。
Android 文档:
https://developer.android.com/guide/topics/ui/layout/gridview.html
https://developer.android.com/reference/android/widget/GridView.html
我花了几个星期四处寻找如何做到这一点,并遇到了 this and this 之类的事情,但它似乎已经有几年了/我无法理解如何应用它到我需要的。
我想要的只是让我的图像按钮位于屏幕上的 4x4 网格中,并且每个按钮都启动一个类似的 activity 但具有不同的数据。 (这是一种闪卡类型的东西,但每张卡片都有不同的文字)。看起来这应该很简单,但这是我的第一个 android 应用程序,也是我第一次直接接触 xml 和 java,我觉得我迷路了。
实现这个的好方法是什么?我不是特别喜欢做一个 gridView,并且对任何有用的东西都持开放态度。
有点像这样:
您可以使用 TableLayout 来实现它。下面的例子。显然,您可以将按钮更改为 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>
您可以使用 GridView。以下 Android 文档 link 包含有关如何使用它的示例和详细信息。
Android 文档: https://developer.android.com/guide/topics/ui/layout/gridview.html
https://developer.android.com/reference/android/widget/GridView.html