android-如何gridview拉伸填充宽度

时间:2015-02-10 14:24:11

标签: android android-listview android-gridview

这是我当前gridview的图像:

enter image description here

这是我想要的: enter image description here

这是我的griview布局:

    <GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:columnWidth="100dp"
    android:drawSelectorOnTop="true"
    android:gravity="center"
    android:numColumns="2"
    android:stretchMode="spacingWidthUniform"
    android:verticalSpacing="5dp" >
</GridView>

这是我的自定义行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
 >

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter" >
</ImageView>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:includeFontPadding="false"
    android:singleLine="true"
    android:lineSpacingExtra="5dp"
    android:textSize="12sp" >
</TextView>

我已经尝试了大约3个小时,我不能,上面的代码是我的最新代码。你可以帮帮我吗 ?如何填充2行以填充宽度?

谢谢

2 个答案:

答案 0 :(得分:0)

您需要使用dp将ImageView layout_width + layout_height从match_parent更改为固定大小。例如,对宽度和高度执行类似60dp的操作

答案 1 :(得分:-1)

您必须在数据(图像和文本)和视图(GridView)之间使用适配器:Adapter

这里有一个很好的示例,其中包含一个扩展BaseAdapter的CustomGrid:Android Custom GridView with Images and Text

希望它有所帮助!

梅西