在gridview中使用图像大小的Android问题

时间:2013-08-02 16:53:58

标签: android android-xml android-gridview

我正在尝试创建类似于Google Play中使用的卡片的网格布局

enter image description here

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

对于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>

for 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>

我得到的输出是:

enter image description here

我使用的图像尺寸为356x500。 (我确实使用300x300获得了一个尺寸稍大的网格) 我面临的问题是:

  1. 1.如图所示,卡片太大,图像周围有很多空白区域,图像不适合 谷歌游戏商店。我该如何使卡片成为比例 图片。白色空间量随图像大小的变化而变化。
  2. 卡片“卡在”屏幕的左边缘。如何在屏幕边缘和卡之间保持间距。
  3. 请帮我纠正这个问题。 感谢

3 个答案:

答案 0 :(得分: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中使用

机器人:stretchMode =&#34;无&#34;而不是android:stretchMode =&#34; columnWidths#34;

答案 1 :(得分:0)

把android:scaleType =&#34; fitStart&#34;在你的

里面

答案 2 :(得分:0)

使用imgIcon.setAdjustViewBounds(true); 你也可以在xml

中做同样的事情