首页 > 解决方案 > 如何将android导航抽屉添加到非空的主要活动?

问题描述

我有一个我一直在做的项目。它已经有很多代码,但我决定要为其添加一个导航抽屉。最好的方法是什么?我是否必须调整现有布局以适应导航抽屉?

这是我试图将导航抽屉添加到https://i.stack.imgur.com/wmDod.png的主片段

这些是 home 片段的 XML 代码:

      ?xml version="1.0" encoding="utf-8"?>
     <androidx.core.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.HomeFragment">


    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.denzcoskun.imageslider.ImageSlider
            android:id="@+id/image_slider"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            app:iss_auto_cycle="true"
            app:iss_delay="0"
            app:iss_period="6000" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="9dp"
            android:orientation="horizontal"
            android:weightSum="2">

            <TextView
                android:id="@+id/Category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:fontFamily="@font/andada"
                android:text="Category"
                android:textSize="21sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/category_see_all"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:fontFamily="@font/andada"
                android:gravity="end"
                android:text="See All"
                android:textColor="@color/teal_700"
                android:textSize="18sp"
                android:textStyle="bold" />

        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rec_category"
            android:layout_width="wrap_content"
            android:layout_height="160dp"
            android:layout_marginLeft="9dp"
            android:layout_marginTop="10dp" />

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_marginTop="10dp"
            android:elevation="6dp"
            app:cardCornerRadius="5dp">


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

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/Recents"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="9dp"
                        android:fontFamily="@font/andada"
                        android:text="Recents "
                        android:textSize="21sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/Recents"
                        android:layout_marginLeft="9dp"
                        android:text="Recently added items for sale." />

                    <TextView
                        android:id="@+id/newProducts_see_all"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_marginRight="8dp"
                        android:fontFamily="@font/andada"
                        android:text="See All"
                        android:textColor="@color/teal_700"
                        android:textSize="18sp"
                        android:textStyle="bold" />
                </RelativeLayout>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/new_product_rec"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="7dp"
                    android:layout_marginTop="6dp"
                    android:padding="5dp" />
            </LinearLayout>

        </androidx.cardview.widget.CardView>

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

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/Popular_Items"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="9dp"
                    android:fontFamily="@font/andada"
                    android:text="Popular Items "
                    android:textSize="21sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/Popular_Items"
                    android:layout_marginLeft="9dp"
                    android:text="Most popular items posted." />

                <TextView
                    android:id="@+id/popular_see_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="8dp"
                    android:fontFamily="@font/andada"
                    android:text="See All"
                    android:textColor="@color/teal_700"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/popular_rec"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="7dp"
                android:layout_marginTop="6dp"
                android:padding="5dp" />
        </LinearLayout>

    </LinearLayout>




    </androidx.core.widget.NestedScrollView>

这些是运行上面显示的主页片段的主要活动的代码:

    <?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=".activities.MainActivity">


    <FrameLayout
        android:id="@+id/home_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

添加导航抽屉会弄乱我现有的代码吗?我需要帮助,谢谢!

标签: androidandroid-studioandroid-fragments

解决方案


推荐阅读