【问题标题】:RecyclerView and gridViewRecyclerView 和 gridView
【发布时间】:2017-08-07 21:28:16
【问题描述】:

我使用recyclerview实现了gridview,问题是图像显示不正确,因为图像之间有太多空间,这里是mainactivity的xml和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>

在主要活动中:

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

结果是这样的

您可以看到两列图像周围有空格,我该如何去除这些空格?

高度后包裹内容

【问题讨论】:

  • 我什么都没做
  • 图像视图和布局的高度和宽度都与父级匹配?
  • 试试 recyclerView.setHasFixedSize(true);在设置适配器之前。
  • 什么都没发生
  • 如果有效,请将其作为答案发布

标签: java android xml gridview android-recyclerview


【解决方案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>

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

【讨论】:

  • 我确实喜欢两张图片都被胶合了,它们之间的空间消失了,但是在垂直方向上,每行之间有很大的空间
  • 不只是访问和设置数据
【解决方案2】:

这样做:

    <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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多