问题
I implemented gridview using recyclerview the problem is that images are not showing properly like there is so much space between images, here is xml of mainactivity and griditem.xml
mainactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="match_parent"/>
</RelativeLayout>
griditem.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:id="@+id/iv"/>
</LinearLayout>
And in mainactivity:
GridLayoutManager grid = new GridLayoutManager(this,2);
recyclerView.setAdapter(new MovieRecyclerAdapter());
recyclerView.setLayoutManager(grid);
and the result is like this
you can see there is space around two columns of image, how can i remove these spaces?
After height to wrap content

回答1:
girditem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
To add spaces between items set the margin to your desired number of dp
回答2:
Do this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content"/>
</RelativeLayout>
Your item xml outer layout should also have the height as wrap_content
来源:https://stackoverflow.com/questions/45555970/recyclerview-and-gridview