android中的Gridview列表项

时间:2015-03-26 14:13:38

标签: android xml gridview

我已将gridview设置为2列,正确显示值,但在显示文本时,如果文本转到第二行,则第1列的大小较小,第2列的大小较大。我不确定为什么会这样?我已经设置了所有Match_Parent。

这会让我知道我想说的是什么:

enter image description here

你注意到红圈了吗?第3行第1列与第3行第2列相比为什么?如何使所有列保持一致?

这是我的gridview.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    tools:ignore="ContentDescription,RtlHardcoded,NestedWeights" >

     <TextView
        android:id="@+id/noData"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/nocontantdata"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="italic"
        android:visibility="gone" />

    <GridView
       android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:horizontalSpacing="2dp"
        android:numColumns="2"
        android:scrollbars="vertical"
        android:verticalSpacing="2dp"
        android:visibility="visible"  >
    </GridView>

    <ImageView
        android:id="@+id/imagefooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/footer_band"
        tools:ignore="ObsoleteLayoutParam" />

</RelativeLayout>

我的itemsRow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/itemsLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/layoutbackground"
    android:gravity="center">

    <ImageView
        android:id="@+id/profileImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="@drawable/friends"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" />

    <TextView
        android:id="@+id/profileText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/profileImage"
        android:gravity="center"
        android:paddingTop="5dp"
        android:textColor="@color/blackText"
        android:text="@string/profile_text"
        android:textSize="16sp" />

</RelativeLayout>

这里可能有什么问题?

谢谢!

2 个答案:

答案 0 :(得分:1)

这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemsLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="@color/layoutbackground"
android:gravity="center">

<ImageView
    android:id="@+id/profileImage"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/friends"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" />

<TextView
    android:id="@+id/profileText"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:paddingTop="5dp"
    android:textColor="@color/blackText"
    android:text="@string/profile_text"
    android:textSize="16sp" />

</LinearLayout>

答案 1 :(得分:1)

如果您只想保持项目大小相同,则只能显示一行文本。将参数maxlines = 1更改/添加到textView,这将使它们保持较小的尺寸。您的项目的相对布局高度应设置为wrap-content,不确定这是否会对您产生影响。