Android Activity 欢迎页面中未显示抽屉菜单

问题描述 投票:0回答:1

我目前在 Android Activity 中遇到主菜单显示问题,特别是在 Activity_welcome.xml 布局文件中。

用户登录后,他们将被定向到 Activity_welcome 页面,该页面应显示欢迎消息,然后显示选项菜单。菜单是使用带有 NavigationView 的 DrawerLayout 实现的。

问题:欢迎消息(在相对布局内)显示正确,但菜单未出现在屏幕上。

以下是相关的XML布局文件:

activity_welcome.xml
:此布局文件包含 DrawerLayout 和菜单的 NavigationView。

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:visibility="visible">

    <!-- Content View -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"><!---->

        <!-- Large Welcome Text -->
        <TextView
            android:id="@+id/welcomeText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="32dp"
            android:gravity="center"
            android:text="Welcome [User Name]"
            android:textSize="24sp"
            android:textStyle="bold" /><!---->

        <!-- User Information -->
        <TextView
            android:id="@+id/userNameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/welcomeText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="User Name: "
            android:textSize="18sp" />

        <TextView
            android:id="@+id/userEmailText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/userNameText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="Email: "
            android:textSize="18sp" />

        <TextView
            android:id="@+id/userIdText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/userEmailText"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="User ID: "
            android:textSize="18sp" /><!---->


    </RelativeLayout><!---->
    <!-- Navigation Drawer -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"
         />
</androidx.drawerlayout.widget.DrawerLayout>

nav_header.xml
:这是导航视图标题的布局。

<?xml version="1.0" encoding="utf-8"?><!-- nav_header.xml -->

<RelativeLayout 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="160dp"
    >

    <!-- Your app's logo or an icon -->
    <ImageView
        android:id="@+id/nav_header_icon"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:src="@mipmap/ic_launcher" />

    <!-- A title or app name -->
    <TextView
        android:id="@+id/nav_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nav_header_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="SongTracker"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold" />
</RelativeLayout>

drawer_menu.xml
:此文件定义导航视图中的菜单项。

<?xml version="1.0" encoding="utf-8"?><!-- nav_header.xml -->

<RelativeLayout 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="160dp"
    >

    <!-- Your app's logo or an icon -->
    <ImageView
        android:id="@+id/nav_header_icon"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:src="@mipmap/ic_launcher" />

    <!-- A title or app name -->
    <TextView
        android:id="@+id/nav_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nav_header_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="SongTracker"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold" />
</RelativeLayout>

只是为了涵盖所有基础 - 这是我的

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SongTracker" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
    <style name="BackgroundLightGray" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/light_gray</item>
    </style>
    <style name="OrangeTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/orange</item>
    </style>
    <style name="AppTheme.NavigationView" parent="Base.Theme.AppCompat.Light">
        <item name="android:background">@color/black</item>
        <item name="android:textColor">@color/white</item>
    </style>


</resources>

采取的故障排除步骤:

  • 我已经确保了可见性属性(android:visibility) 对于 DrawerLayout 和relativelayout都设置为 “可见”。

  • 我已经为
    定义了一个自定义主题(AppTheme.NavigationView) NavigationView 指定文本和背景颜色(参见上面的 theme.xml)。

  • 使用 Android Studio 的布局查看了我的布局层次结构
    检查员没有发现任何重叠的视图或问题。

  • 在不同的模拟器和设备上测试了该应用程序以排除 特定于设备的问题。

  • 检查了我的 Java 代码是否有任何编程条件
    影响 NavigationView 的可见性。

请求协助:

I kindly request assistance in identifying and resolving the issue causing the main menu not to display in the activity_welcome layout. Any insights, suggestions, or troubleshooting steps would be greatly appreciated.

提前感谢您的帮助!

java android xml drawerlayout
1个回答
0
投票

第一个菜单 xml 文件与布局文件不同,您只需更改它并使其像这样,我的意思是

drawer_menu.xml
:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/your_id"
        android:title="Title"
        android:icon="@mipmap/ic_launcher"  
        android:showAsAction="always" />

</menu>

并在抽屉布局中添加

openDrawer
属性:

<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_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:openDrawer="start">
© www.soinside.com 2019 - 2024. All rights reserved.