导航抽屉在 Android Studio 中不起作用

Posted

技术标签:

【中文标题】导航抽屉在 Android Studio 中不起作用【英文标题】:Navigation Drawer not working in Android Studio 【发布时间】:2021-12-17 13:28:33 【问题描述】:

我真的很困惑如何在我的应用程序中实现导航侧边栏,但我已经尝试过了,但它会发生冲突,如果我实现了它,它会破坏我的设计XML。冲突是当我在activity_main 下将constraintLayout 更改为DrawLayout 时,我的设计将会崩溃,但是在实现导航侧边栏时我需要使用DrawLayout 而不是ConstraintLayout

拳头图像是主要的XML 普通用户界面,第二张图像描述了当我尝试将我的constraintlayout 更改为drawerlayout 时的输出activity main,有些设计没有出现,但导航侧边栏的效果与第三张图片中的一样。问题是可以使用 constraintlayout 代替 drawerlayout 吗?避免拆掉 UI 设计?或者也许还有其他方法可以使用drawerLayout 查看设计。

Activitymain.xml 这是我在第二张图片中使用的 drawerlayout 导航侧边栏在这方面效果很好,但设计被拆掉了。但是当我使用 <androidx.constraintlayout.widget.ConstraintLayout constraintlayout 时,UI 布局不会拆除,但它会发生冲突并且应用程序崩溃,因为在 MainActivity 它使用 ActionBarDrawerToggle 。我希望任何人都可以帮助我解决这个问题

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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_
android:layout_
android:background="@color/background"
android:id="@+id/drawerlayout"
tools:context=".MainActivity">

<com.google.android.material.navigation.NavigationView
    android:layout_
    android:layout_
    android:id="@+id/navigationview"
    app:headerLayout="@layout/sidebar_header"
    app:menu="@menu/sidemenu"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_gravity="start"/>


<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_
    android:layout_
    android:background="@color/background"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="Toolbars"
    app:titleTextColor="#FFFFFF"
    app:titleMarginStart = "10dp"
    android:layout_marginBottom="5dp"
    tools:ignore="MissingConstraints"/>


<View
    android:id="@+id/viewHeaderBackground"
    android:layout_
    android:layout_
    android:background="@color/primary"
    app:layout_constraintTop_toTopOf="parent"/>

<com.github.ybq.android.spinkit.SpinKitView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/spin_kit"
    style="@style/SpinKitView.Large.Circle"
    android:layout_
    android:layout_
    android:layout_gravity="center"
    app:SpinKit_Color="@color/validation"
    tools:ignore="MissingConstraints" />



<TextView
    android:id="@+id/textTitle"
    android:layout_
    android:layout_
    android:layout_marginStart="@dimen/_16sdp"
    android:layout_marginLeft="@dimen/_16sdp"
    android:layout_marginTop="@dimen/_16sdp"
    android:text="Cash Grants"
    android:textColor="@color/white"
    android:textSize="@dimen/_20ssp"
    android:textStyle="bold"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>


<androidx.appcompat.widget.AppCompatImageView
    android:layout_
    android:layout_
    android:layout_marginEnd="@dimen/_16sdp"
    android:layout_marginRight="@dimen/_16sdp"
    android:src="@drawable/dswd_logo"
    app:layout_constraintBottom_toBottomOf="@id/textTitle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@id/textTitle"
    app:tint="@color/white"
    />


<com.google.android.material.card.MaterialCardView
    android:id="@+id/cardHeader"
    android:layout_
    android:layout_
    android:layout_marginStart="@dimen/_16sdp"
    android:layout_marginEnd="@dimen/_16sdp"
    app:cardBackgroundColor="@color/card_background"
    app:cardCornerRadius="@dimen/_16sdp"
    app:layout_constraintBottom_toBottomOf="@id/viewHeaderBackground"
    app:layout_constraintTop_toBottomOf="@id/viewHeaderBackground">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_
        android:padding="@dimen/_14sdp">
        <TextView
            android:id="@+id/textView"
            android:layout_
            android:layout_
            android:text="Overview"
            android:textColor="@color/primary_text"
            android:textSize="@dimen/_14ssp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <ImageButton
            android:id="@+id/textViews"
            android:layout_
            android:layout_
            android:layout_weight="2"
            android:background="@android:color/transparent"

            android:scaleType="fitCenter"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
        <LinearLayout
            android:id="@+id/layoutClients"
            android:layout_
            android:layout_
            android:layout_marginTop="@dimen/_8sdp"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintEnd_toStartOf="@id/layoutImpacted"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textViews">

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_
                android:layout_
                android:src="@drawable/ic_clients"/>

            <TextView
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/_4sdp"
                android:text="Sync"
                android:textColor="@color/secondary_text"
                android:textSize="@dimen/_10ssp"/>

            <TextView
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/_4sdp"
                android:text="0"
                android:textColor="@color/primary_text"
                android:textSize="@dimen/_16ssp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutImpacted"
            android:layout_
            android:layout_
            android:layout_marginTop="@dimen/_8sdp"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintEnd_toStartOf="@id/layoutFollowing"
            app:layout_constraintStart_toEndOf="@id/layoutClients"
            app:layout_constraintTop_toBottomOf="@id/textViews">

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_
                android:layout_
                android:src="@drawable/ic_impacted"/>

            <TextView
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/_4sdp"
                android:text="Pending"
                android:textColor="@color/secondary_text"
                android:textSize="@dimen/_10ssp"/>

            <TextView
                android:id="@+id/txtPending"
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/_4sdp"
                android:text="0"
                android:textColor="@color/primary_text"
                android:textSize="@dimen/_16ssp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutFollowing"
            android:layout_
            android:layout_
            android:layout_marginTop="@dimen/_8sdp"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/layoutImpacted"
            app:layout_constraintTop_toBottomOf="@id/textViews">

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_
                android:layout_
                android:src="@drawable/ic_following"/>

            <TextView
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/_4sdp"
                android:text="Spam"
                android:textColor="@color/secondary_text"
                android:textSize="@dimen/_10ssp"/>

            <TextView
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/_4sdp"
                android:text="0"
                android:textColor="@color/primary_text"
                android:textSize="@dimen/_16ssp"/>
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

<ScrollView
    android:layout_
    android:layout_
    android:clipToPadding="false"
    android:overScrollMode="never"
    android:padding="@dimen/_16sdp"
    android:scrollbars="none"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/cardHeader">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <include layout="@layout/dashboard_item_1"/>
        <include layout="@layout/dashboard_item_2"/>
        <include layout="@layout/dashboard_item_3"/>
        <include layout="@layout/dashboard_item_4"/>
    </LinearLayout>
    
</ScrollView>

</androidx.drawerlayout.widget.DrawerLayout >

MainActivity

 protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawerlayout = findViewById(R.id.drawerlayout);
    navigationView = findViewById(R.id.navigationview);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerlayout,toolbar,R.string.navigation_open,R.string.navigation_close);
    drawerlayout.addDrawerListener(toggle);
    toggle.syncState();

【问题讨论】:

【参考方案1】:

问题是可以使用约束布局而不是抽屉布局

是的。

您只需要确保您的设计骨架看起来像:

<DrawerLayout>

    <NavigationView>

    <!--Main layout-->
    <ConstraintLayout>
         <view1 inside ConstraintLayout>
         <view2 inside ConstraintLayout>
         ....

但是你所做的是将ConstraintLayout 替换为DrawerLayout,这样视图就无法在未知约束下正确布局:

<DrawerLayout>

    <NavigationView>

    <view1 inside ConstraintLayout>
    <view2 inside ConstraintLayout>
    ....

所以,在你的布局中应用它:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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_
android:layout_
android:background="@color/background"
android:id="@+id/drawerlayout"
tools:context=".MainActivity">

    <com.google.android.material.navigation.NavigationView
        android:layout_
        android:layout_
        android:id="@+id/navigationview"
        app:headerLayout="@layout/sidebar_header"
        app:menu="@menu/sidemenu"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_gravity="start"/>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_>
        
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_
            android:layout_
            android:background="@color/background"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:title="Toolbars"
            app:titleTextColor="#FFFFFF"
            app:titleMarginStart = "10dp"
            android:layout_marginBottom="5dp"
            tools:ignore="MissingConstraints"/>


        <View
            android:id="@+id/viewHeaderBackground"
            android:layout_
            android:layout_
            android:background="@color/primary"
            app:layout_constraintTop_toTopOf="parent"/>

        <com.github.ybq.android.spinkit.SpinKitView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/spin_kit"
            style="@style/SpinKitView.Large.Circle"
            android:layout_
            android:layout_
            android:layout_gravity="center"
            app:SpinKit_Color="@color/validation"
            tools:ignore="MissingConstraints" />



        <TextView
            android:id="@+id/textTitle"
            android:layout_
            android:layout_
            android:layout_marginStart="@dimen/_16sdp"
            android:layout_marginLeft="@dimen/_16sdp"
            android:layout_marginTop="@dimen/_16sdp"
            android:text="Cash Grants"
            android:textColor="@color/white"
            android:textSize="@dimen/_20ssp"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>


        <androidx.appcompat.widget.AppCompatImageView
            android:layout_
            android:layout_
            android:layout_marginEnd="@dimen/_16sdp"
            android:layout_marginRight="@dimen/_16sdp"
            android:src="@drawable/dswd_logo"
            app:layout_constraintBottom_toBottomOf="@id/textTitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/textTitle"
            app:tint="@color/white"
            />


        <com.google.android.material.card.MaterialCardView
            android:id="@+id/cardHeader"
            android:layout_
            android:layout_
            android:layout_marginStart="@dimen/_16sdp"
            android:layout_marginEnd="@dimen/_16sdp"
            app:cardBackgroundColor="@color/card_background"
            app:cardCornerRadius="@dimen/_16sdp"
            app:layout_constraintBottom_toBottomOf="@id/viewHeaderBackground"
            app:layout_constraintTop_toBottomOf="@id/viewHeaderBackground">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_
                android:layout_
                android:padding="@dimen/_14sdp">
                <TextView
                    android:id="@+id/textView"
                    android:layout_
                    android:layout_
                    android:text="Overview"
                    android:textColor="@color/primary_text"
                    android:textSize="@dimen/_14ssp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>

                <ImageButton
                    android:id="@+id/textViews"
                    android:layout_
                    android:layout_
                    android:layout_weight="2"
                    android:background="@android:color/transparent"

                    android:scaleType="fitCenter"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>
                <LinearLayout
                    android:id="@+id/layoutClients"
                    android:layout_
                    android:layout_
                    android:layout_marginTop="@dimen/_8sdp"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toStartOf="@id/layoutImpacted"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/textViews">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_
                        android:layout_
                        android:src="@drawable/ic_clients"/>

                    <TextView
                        android:layout_
                        android:layout_
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="Sync"
                        android:textColor="@color/secondary_text"
                        android:textSize="@dimen/_10ssp"/>

                    <TextView
                        android:layout_
                        android:layout_
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="0"
                        android:textColor="@color/primary_text"
                        android:textSize="@dimen/_16ssp"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/layoutImpacted"
                    android:layout_
                    android:layout_
                    android:layout_marginTop="@dimen/_8sdp"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toStartOf="@id/layoutFollowing"
                    app:layout_constraintStart_toEndOf="@id/layoutClients"
                    app:layout_constraintTop_toBottomOf="@id/textViews">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_
                        android:layout_
                        android:src="@drawable/ic_impacted"/>

                    <TextView
                        android:layout_
                        android:layout_
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="Pending"
                        android:textColor="@color/secondary_text"
                        android:textSize="@dimen/_10ssp"/>

                    <TextView
                        android:id="@+id/txtPending"
                        android:layout_
                        android:layout_
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="0"
                        android:textColor="@color/primary_text"
                        android:textSize="@dimen/_16ssp"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/layoutFollowing"
                    android:layout_
                    android:layout_
                    android:layout_marginTop="@dimen/_8sdp"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@id/layoutImpacted"
                    app:layout_constraintTop_toBottomOf="@id/textViews">

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_
                        android:layout_
                        android:src="@drawable/ic_following"/>

                    <TextView
                        android:layout_
                        android:layout_
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="Spam"
                        android:textColor="@color/secondary_text"
                        android:textSize="@dimen/_10ssp"/>

                    <TextView
                        android:layout_
                        android:layout_
                        android:layout_marginTop="@dimen/_4sdp"
                        android:text="0"
                        android:textColor="@color/primary_text"
                        android:textSize="@dimen/_16ssp"/>
                </LinearLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.card.MaterialCardView>

        <ScrollView
            android:layout_
            android:layout_
            android:clipToPadding="false"
            android:overScrollMode="never"
            android:padding="@dimen/_16sdp"
            android:scrollbars="none"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/cardHeader">

            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="vertical">

                <include layout="@layout/dashboard_item_1"/>
                <include layout="@layout/dashboard_item_2"/>
                <include layout="@layout/dashboard_item_3"/>
                <include layout="@layout/dashboard_item_4"/>
            </LinearLayout>
            
        </ScrollView>
        
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.drawerlayout.widget.DrawerLayout >

【讨论】:

以上是关于导航抽屉在 Android Studio 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

navigation.goBack() 在嵌套导航中不起作用

在 Android Studio 中实现导航抽屉的问题

android studio 不创建导航抽屉活动

在 Android Studio 的默认导航抽屉模板中使用按钮在片段之间切换

在 Android Studio 默认模板中更改导航抽屉图标的颜色

如何在所有活动中使用Android Studio默认导航抽屉[重复]