滚动时GridView会断开吗?

时间:2017-10-18 10:18:41

标签: java android gridview

我正在开发一款将展示女孩时尚类别的应用程序。我试图在网格视图中显示类别。但是当我测试应用程序时,网格视图在我滚动时会断开。以下是我的代码。请帮帮我。提前谢谢。

enter image description here

single_item.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:gravity="center">

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:id="@+id/grid_image"/>

<TextView
    android:maxLines="1"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/grid_text"
    android:textColor="#000"
    android:textSize="20sp"
    android:gravity="center"/>

</LinearLayout>

activity_browse.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.beautyworld.BrowseActivity">


<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="150dp"
    android:layout_height="match_parent">

</GridView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以将RelativeLayout用于此目的而不是LinearLayout

single_item.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#2a2a2a" >

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:id="@+id/grid_image"/>

<TextView
    android:maxLines="1"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/grid_text"
    android:textColor="#000"
    android:textSize="20sp"
    android:gravity="center"/>

</RelativeLayout>

activity_browse.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#2a2a2a" >

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="150dp"
    android:layout_height="match_parent">

</GridView>

</RelativeLayout>

此答案已提供here