0

我正在尝试创建类似于 google play 中使用的卡片网格布局,如下所示

在此处输入图像描述

我正在使用的布局和获得的布局是:

对于 GridView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#e5e5e5"
     >

    <GridView  
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="60dp"
    android:numColumns="2"
    android:layout_marginLeft="2dp"

    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

</RelativeLayout>

对于 GridItem:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_card"

>

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
     />

  <TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    android:textSize="17sp"
    android:layout_below="@id/imgIcon"
    android:layout_marginTop="35dp"
    android:layout_centerInParent="true"/>
</RelativeLayout>

我得到的输出是这样的:

在此处输入图像描述

我使用的图像尺寸是 356x500。(我在使用 300x300 时确实得到了一个大小合适的网格)我面临的问题是:

  1. 1.如图所示,卡片太大,图像周围有很多空白,图像不适合 google play 商店。我如何使卡片与图像成比例。空白的数量随着图像大小的变化而变化。
  2. 卡片“卡”在屏幕的左边缘。我如何保持屏幕边缘和卡片之间的间距。

请帮助我纠正这个问题。谢谢

4

3 回答 3

0

将 android:scaleType="fitStart" 放入您的

于 2014-12-26T18:24:55.960 回答
0

空间是因为你用过

android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"

同时编辑您的

<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
     />

在 gridview 中使用 android:stretchMode="none" 而不是 android:stretchMode="columnWidth"

于 2013-08-02T17:03:16.183 回答
0

使用imgIcon.setAdjustViewBounds(true); 你也可以在xml中做同样的事情

于 2017-03-25T06:40:27.180 回答