2017-10-18 74 views
0

我正在開發一個應用程序,將顯示女孩時尚類別。我試圖在網格視圖中顯示類別。但是當我測試應用程序時,Grid View在我滾動它的時候正在崩潰。以下是我的代碼。請幫助我。提前致謝。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> 

回答

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

+0

我仍然面臨問題 –