以编程方式以不同方式对齐RecyclerViewer中的每个项目自定义网格布局

问题描述

当行索引> 1时,我实现了具有自定义网格布局的回收器查看器,该布局具有一个标题和每行3个项目。

我的问题是如何以编程方式将每行中的三项与标题中某些元素的宽度对齐:

enter image description here

例如,如何确定所有空闲的狗以适应hello world框的宽度(红线)并在其正下方对齐,以得到这样的结果(通过图像编辑工具进行编辑):

enter image description here

有什么想法吗?

标题xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
    android:background="@color/lightgrey"
    android:orientation="vertical">     
     
     <LinearLayout
                android:id="@+id/aboutme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/circularbordersolid"
                android:orientation="vertical"
                android:padding="16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/proximanovaregular"
                            android:text="@string/hello"
                            android:textSize="17sp"
                            android:textStyle="bold" />

                        <Space
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="10dp"
                            android:fontFamily="@font/proximanovaregular"
                            android:text="Edit"
                            android:textColor="#FFCF66"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="@string/value" />
                </LinearLayout>

            </LinearLayout>
</RelativeLayout>

图片xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:layout_height="wrap_content"

    android:background="@color/grey">

    <androidx.cardview.widget.CardView
        android:id="@+id/recipeCard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey"
        app:cardCornerRadius="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageViewRecipe"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="1dp"
            android:layout_gravity="right"
            android:layout_marginBottom="0dp"
            android:background="@color/grey"
            app:srcCompat="@drawable/dog" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)