RecyclerView项的高度和宽度会动态更改Android [英] RecyclerView item height and width change dynamically Android
问题描述
我是android开发的新手,我正在开发一个应用程序,其中该应用程序从服务器下载图片并将其显示在gridView中.但是,当应用程序在更大屏幕的手机上运行时,还会出现一些额外的空间.这是一些屏幕截图.我该如何解决?我有每个项目的模型,高度和宽度固定为110dp
I am new to android development and I am developing an app where the app downloads pictures from server and show them in a gridView. But some extra spaces are coming up when the app is run on a bigger screen phone. Here are some screenshots. How do I solve this ? I have the model of each item with a fixed height and width of 110dp
我的图像模型:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="110dp"
android:layout_height="110dp"
app:srcCompat="@drawable/post2"
android:id="@+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_margin="1.5dp" />
我的回收者视图:
<android.support.v7.widget.RecyclerView
android:id="@+id/gallerygrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:visibility="visible"
android:layout_gravity="center">
</android.support.v7.widget.RecyclerView>
推荐答案
根据屏幕尺寸动态设置高度和宽度.这是获得完美结果的最佳方法.
Set height and width dynamically according to the screen size. this is the best way to get perfect result.
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//if you need three fix imageview in width
int devicewidth = displaymetrics.widthPixels / 3;
//if you need 4-5-6 anything fix imageview in height
int deviceheight = displaymetrics.heightPixels / 4;
holder.image_view.getLayoutParams().width = devicewidth;
//if you need same height as width you can set devicewidth in holder.image_view.getLayoutParams().height
holder.image_view.getLayoutParams().height = deviceheight;
在适配器中的getview中尝试此操作,希望您在所有设备上都能获得更好的结果
try this in adapter in getview hope you get better result in all device
或设置与宽度相同的高度,您也可以如下设置高度的设备宽度:
Or to set same height same as width you can set devicewidth in height also like below:
holder.image_view.getLayoutParams().height = devicewidth;
这篇关于RecyclerView项的高度和宽度会动态更改Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!