Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RecyclerView项目高度和宽度动态更改_Android_Android Recyclerview - Fatal编程技术网

Android RecyclerView项目高度和宽度动态更改

Android RecyclerView项目高度和宽度动态更改,android,android-recyclerview,Android,Android Recyclerview,我是android开发的新手,我正在开发一个应用程序,该应用程序从服务器下载图片并在gridView中显示。但当应用程序在大屏幕手机上运行时,会出现一些额外的空间。这里是一些截图。我如何解决这个问题?我有固定高度和宽度为110dp的每个项目的模型 我的图像模型: <?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/andro

我是android开发的新手,我正在开发一个应用程序,该应用程序从服务器下载图片并在gridView中显示。但当应用程序在大屏幕手机上运行时,会出现一些额外的空间。这里是一些截图。我如何解决这个问题?我有固定高度和宽度为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>


根据屏幕大小动态设置高度和宽度。这是获得完美结果的最佳方法

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的适配器中尝试此操作,希望在所有设备中都能获得更好的结果

编辑: 或者,要将高度设置为宽度,您可以将devicewidth设置为高度,如下所示:

holder.image_view.getLayoutParams().height = devicewidth;

对于那些希望在xml布局(recyclerView项)文件中执行此操作的人,可以使用ConstraintLayout 结合

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
下面是一个正方形项目的代码

            app:layout_constraintDimensionRatio="4:4"
别忘了提供您可以设置的所有约束

            android:layout_width="0dp"
            android:layout_height="0dp"
因此,完整的代码如下

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginEnd="1dp"
        >

        <ImageView
            android:id="@+id/img_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            app:layout_constraintDimensionRatio="4:4"
            app:srcCompat="@drawable/event_placeholder"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:scaleType="centerCrop"
            />
</androidx.constraintlayout.widget.ConstraintLayout>

您还可以在测量后获得
RecyclerView
宽度。见

比如说,

recyclerView.post {
    val width = recyclerView.width
    ...
}

显示您的xml文件为大屏幕手机创建xml布局是必要的。我刚刚发布了代码如果您的高度是固定的,请计算并保持宽高比,或者设置为全宽width@ShakilMahmudShanto请检查下面我的答案,它可以帮助您并在所有设备中给出正确的结果,如果您对上述代码有任何问题请随时提问:):)我在两个活动中使用我的recyclerView来表示(垂直和水平)。我可以在活动中使用此代码而不是在适配器中使用吗?@ZakiPathan您的意思是将此代码放在onBindViewHolder而不是getview中吗?谢谢,这也适用于android.support.constraint.ConstraintLayout。