如何在两列上创建带有正方形项的GridView?

时间:2013-05-21 09:49:42

标签: android android-layout gridview

我想在两列上创建一个带有正方形项的GridView,它将占用整个屏幕(match_parent)。

每个项目都包含一个imageView和一个textView。

以下是图片中的结果:

enter image description here

如何使项目宽度等于其可变高度?

提前谢谢!!

3 个答案:

答案 0 :(得分:1)

在您的活动或片段布局中,您应该像这样使用GridView:

<GridView
        android:id="@+id/grid_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
         />

然后你应该有一个扩展BaseAdapter的适配器,它的项目应该如下:

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableTop="@drawable/yourDrawable"
        android:layout_margin="20dip"
        android:gravity="center" />

您需要更多解释吗?

答案 1 :(得分:0)

使用这些链接,他们会帮助您 Option 1

Option 2

答案 2 :(得分:0)

这是Gridview。

<GridView
    android:numColumns="2"
    android:id="@+id/related_contact_grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

这是项目。您将在Adapter中将其充气到gridview。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView"
        android:text="Tendance"
        android:layout_centerHorizontal="true"

</RelativeLayout>

如果您愿意,我可以支持您在此Gridview上的工作。