繁体   English   中英

如何在 gridview android 工作室在一个视图中制作 2 个布局

[英]How to make 2 layout in one view at gridview android studio

我将在另一个布局下使用网格视图制作菜单。 我使用 recyclerview 制作了 gridview 但无法显示顶部布局我的代码是

activity_grid_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>

grid_layout.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>

结果

结果 2

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

不要在grid_layout.xml包含您的LinearLayout ,而是在 recyclerView 上方的activity_grid_layout.xml中添加这个

完成上述更改后,您的布局如下:

activity_grid_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>

grid_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="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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM