如何在gridview android studio中在一个视图中创建2个布局

u4vypkhs  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(500)

我将在另一个布局下使用网格视图制作菜单。我使用recyclerview创建了gridview,但无法显示顶层布局我的代码是
活动\u grid\u layout.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="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.HomeActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/dataList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

网格布局.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:id="@+id/layoutMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10sp"
            android:layout_marginEnd="10sp"
            android:layout_marginTop="20sp"
            android:layout_marginBottom="5sp"
            android:layout_gravity="center_horizontal"
            app:cardCornerRadius="8sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="20sp"
                android:paddingBottom="20sp"
                android:gravity="center"
                android:layout_gravity="center_vertical|center_horizontal">
                <ImageView
                    android:id="@+id/image_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_home"/>
                <TextView
                    android:id="@+id/text_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/news"
                    android:textSize="10sp"
                    android:textStyle="bold"
                    android:textAlignment="center"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

结果呢

请帮我解决这个问题,因为我是android开发者的初学者谢谢

5w9g7ksd

5w9g7ksd1#

不包括您的 LinearLayout 内部 grid_layout.xml 把这个放进去 activity_grid_layout.xml 就在recyclerview上面
完成上述操作后,您的布局将发生以下变化:
活动\u grid\u layout.xml

<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="match_parent"
        android:layout_height="match_parent"
        tools:context=".main.HomeActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:id="@+id/layoutImg"
            android:padding="20dp"
            android:orientation="vertical"
            android:background="@color/sunflower_black"
            tools:ignore="MissingConstraints">

        </LinearLayout>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dataList"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@+id/layoutImg"
            app:layout_constraintBottom_toBottomOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

网格布局.xml:

<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="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/layoutMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            tools:ignore="MissingConstraints">

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginStart="10sp"
                android:layout_marginTop="20sp"
                android:layout_marginEnd="10sp"
                android:layout_marginBottom="5sp"
                app:cardCornerRadius="8sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingTop="20sp"
                    android:paddingBottom="20sp">

                    <ImageView
                        android:id="@+id/image_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_home" />

                    <TextView
                        android:id="@+id/text_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/news"
                        android:textAlignment="center"
                        android:textSize="10sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

相关问题