【问题标题】:Issue with implementing a Navigation Drawer in Android Studio在 Android Studio 中实现导航抽屉的问题
【发布时间】:2021-10-21 09:07:37
【问题描述】:

我一直在尝试使用this 教程系列来实现导航抽屉。 这是一个相当古老的教程,它使用 android.support 库而不是 androidx,所以我尝试将其调整为使用 androidx,但在 UI 和抽屉的功能中存在一些错误。
当我在模拟器或物理设备上启动应用程序时,抽屉看起来像这样(占据整个屏幕)并且它确实从左侧滑入和滑出。

如果有人能指出我做错了什么或建议指导如何以另一种方式实现导航抽屉,那将非常有帮助。

This is how the UI looks

这是我的代码:
All code
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mdnavigationdrawer">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


我在我的 Themes.xml 文件中添加了这个样式:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>


activity_main.xml:

<?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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

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

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/design_default_color_primary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>


nav_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="176dp"
    android:background="@color/design_default_color_secondary"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="@string/NameHere"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/EmailHere" />

</LinearLayout>


drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_message"
            android:icon="@drawable/ic_message"
            android:title="@string/Message" />
        <item
            android:id="@+id/nav_chat"
            android:icon="@drawable/ic_chat"
            android:title="@string/Chat" />
        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/ic_profile"
            android:title="@string/Profile" />
    </group>

    <item android:title="@string/Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_share"
                android:title="@string/Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_send"
                android:title="@string/Send" />
        </menu>
    </item>

</menu>

【问题讨论】:

    标签: java android xml navigation drawer


    【解决方案1】:

    首先你需要检查Linear Layout的宽度,设置为wrap_content

    科特林:

    这是 2020 年的指南,向您展示如何操作:Medium Article 视频指南:Youtube

    Java:

    Youtube

    Article

    【讨论】:

    • 您提到的两个指南都是用 Kotlin 编写的。我能用java实现吗?
    • 我用 Java References 编辑了答案 :)
    • 是的,你可以,你会很快掌握它的窍门
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2020-01-15
    相关资源
    最近更新 更多