【问题标题】:How to create GridView with squares items on two columns?如何在两列上创建带有正方形项目的 GridView?
【发布时间】:2013-05-16 01:02:13
【问题描述】:

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

每个项目都由一个 imageView 和一个 textView 组成。

这是图像中的结果:

如何使项目的宽度等于它们的可变高度?

提前谢谢你!!

【问题讨论】:

标签: android android-layout gridview


【解决方案1】:

这是网格视图。

<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 上的工作。

【讨论】:

    【解决方案2】:

    在您的活动或片段布局中,您应该有这样的 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" />
    

    你需要更多解释吗?

    【讨论】:

      【解决方案3】:

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

      Option 2

      【讨论】:

      • 永远不要只将链接作为您的答案,因为这些链接通常会失效,因此您的答案是无用的。要么解释你的解决方案,要么根本不回答。
      猜你喜欢
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多