有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RecyclerView和gridView

我使用recyclerview实现了gridview。问题是图像没有正确显示,就像图像之间有太多空间一样,这里是mainactivity和griditem的xml。xml

主要活动。xml

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">

<安卓.support.v7.widget.Toolbar
    安卓:id="@+id/toolbar"
    安卓:layout_width="match_parent"
    安卓:layout_height="?attr/actionBarSize"
    安卓:background="?attr/colorPrimary"
    安卓:elevation="4dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<安卓.support.v7.widget.RecyclerView
    安卓:id="@+id/rv"
    安卓:layout_below="@+id/toolbar"
    安卓:layout_width="match_parent"
    安卓:scrollbars="vertical"
    安卓:layout_height="match_parent"/>
</RelativeLayout>

griditem。xml:

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content">

<ImageView
    安卓:layout_width="200dp"
    安卓:layout_height="200dp"
    安卓:layout_gravity="center"
    安卓:id="@+id/iv"/>
</LinearLayout>

在主要活动中:

GridLayoutManager grid = new GridLayoutManager(this,2);
recyclerView.setAdapter(new MovieRecyclerAdapter());
recyclerView.setLayoutManager(grid);

结果是这样的enter image description here

您可以看到图像的两列周围有空格,如何删除这些空格

包装内容的高度之后enter image description here


共 (2) 个答案

  1. # 1 楼答案

    这样做:

        <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>
    

    项目xml外部布局的高度也应为wrap_content

  2. # 2 楼答案

    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>
    

    要在项目之间添加空格,请将边距设置为所需的dp数