Android 如何制作一个;“完美”;2使用RecyclerView和单个ImageView的网格布局

Android 如何制作一个;“完美”;2使用RecyclerView和单个ImageView的网格布局,android,android-recyclerview,Android,Android Recyclerview,我正在尝试使用RecyclerView和GridView作为布局管理器来制作一个2网格布局。到目前为止,我在xml文件的相对布局下有一个单独的图像视图,但它从来没有“完美”对齐到中心。另外,当尝试在xml文件设计选项卡中拖动单个图像视图时,当我尝试使其更“中心化”(不允许我将图像拖动到正确的位置)时,图像会“跳跃” 为了使具有单个图像视图的2栅格居中,我需要更改什么 这是单个项目的xml代码: <RelativeLayout xmlns:android="http://schema

我正在尝试使用
RecyclerView
GridView
作为布局管理器来制作一个2网格布局。到目前为止,我在
xml
文件的相对布局下有一个单独的图像视图,但它从来没有“完美”对齐到中心。另外,当尝试在
xml
文件设计选项卡中拖动单个图像视图时,当我尝试使其更“中心化”(不允许我将图像拖动到正确的位置)时,图像会“跳跃”

为了使具有单个图像视图的2栅格居中,我需要更改什么

这是单个项目的xml代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView               
            android:id="@+id/icon"
            android:layout_width="118dp"
            android:layout_height="118dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:layout_marginTop="12dp"
            android:contentDescription="TODO"
            android:src="@drawable/ic_launcher_background" />
    </RelativeLayout>

这是RecyclerView xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.166"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

不要为
图像视图
指定宽度,试着使其与父对象匹配
而不要为
图像视图
指定宽度,试着使其与父对象匹配
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ImageView               
        android:id="@+id/icon"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="TODO"
        android:scaleType="centerInside"
        android:src="@drawable/ic_launcher_background" />
</android.support.constraint.ConstraintLayout>
试试这个,不管它是否对你有帮助。



试试这个,不管它是否对你有帮助。

在我的代码中应该用什么来代替
frame2
?因为对我来说,它没有定义在我的代码中应该用什么来代替
frame2
?因为对我来说,它还没有定义。问题解决了吗?问题解决了吗?