【发布时间】:2021-06-18 08:25:20
【问题描述】:
我有一个由 RecyclerView 和 GridLayoutManager 生成的网格,所以在我的情况下,网格上总是只有 9 个项目,所以我希望它们填满整个屏幕。以下是它们在我的屏幕上的外观:
这是activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
那么,single_item.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:padding="1dp"
>
<ImageView
android:id="@+id/image_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
/>
</RelativeLayout>
我是如何附加 Gridlayoutmanager 的:
rv = findViewById(R.id.rv);
ImageAdapter imageAdapter = new ImageAdapter(allBitmaps, MainActivity.this);
mLayoutManager = new GridLayoutManager(MainActivity.this, 3);
rv.setLayoutManager(mLayoutManager);
rv.setAdapter(imageAdapter);
所以我希望网格能够很好地适应整个屏幕,如何才能做到最好?
【问题讨论】:
-
贴合屏幕会让它们失去宽高比
-
@Zain,将为您提供适合屏幕的解决方案,我们可能会解决纵横比
标签: java android android-recyclerview bitmap android-adapter