【问题标题】:My NavigationView won't show in DrawerLayout in Android Studio我的 NavigationView 不会显示在 Android Studio 的 DrawerLayout 中
【发布时间】:2018-11-19 14:04:14
【问题描述】:

这是我第一次在 android studio 中创建导航,我已经创建了导航标题和抽屉菜单(它们都显示在自己的 xml 中),但它们都没有出现在我的 activity_home.xml 中(仅 Android..DrawerLayout文本出现),它甚至说我的标题是空的,我错过了什么吗?

这是activity_home.xml

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

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

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:id="@+id/nav_view"
    app:headerLayout="@layout/nav_heqader" *(This line said that xml tag has empty body)*
    app:menu="@menu/drawer_menu"></android.support.design.widget.NavigationView>

这是 nav_heqader.xml(这里有点错字)

>

<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="Test"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Testing"/>

这是drawer_menu.xml

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

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_dashboard_black_24dp"
            android:title="Profilez" />
        <item
            android:id="@+id/nav_share2"
            android:icon="@drawable/ic_dashboard_black_24dp"
            android:title="Profilezd" />
    </menu>
</item>

这是 Home.java

package com.example.aldyro.movie;


import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;

public class Home extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = findViewById(R.id.drawer_layout);

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }


}

这是styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

这是模块:app(以防我的版本不兼容)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.aldyro.movie"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

很抱歉,我是新来的,所以无法直接显示图片

nav_heqader screenshot

drawer_menu screenshot

activity_home screenshot

【问题讨论】:

  • 你说的是Android Studio中的布局编辑器吗?或者你的意思是当你运行应用程序时你的抽屉不起作用?
  • 我认为它是唯一的预览问题。运行时它应该在设备中工作。在预览中也可能会出现一些渲染问题,重新构建项目可以解决这个问题

标签: java android xml android-studio android-layout


【解决方案1】:

您可以从可用模板创建新活动并删除旧活动。

To insert new activity with drawer select File -> New -> acitivity -> Navigation drawer activity. 

您需要根据需要更改菜单条目。

【讨论】:

    【解决方案2】:

    有很多关于这个问题的文档。

    此链接提供在 Fragment 中使用 Navigation Drawer 的文档,而不是您要求的 Activity,但它非常有帮助,如果您是 Android 新手,这是一个很好的起点。

    https://guides.codepath.com/android/Fragment-Navigation-Drawer

    【讨论】:

    • 谢谢!我稍后看看
    【解决方案3】:

    当您从屏幕左侧滑动时会显示抽屉和导航菜单。

    Swipe from left to show the drawer(图片链接)

    你需要设置DisplayShowHomeEnabled 并在点击时您需要管理抽屉 打开和关闭抽屉。

    Video link to your code

    【讨论】:

    • 我已经添加了视频链接。和你上面写的代码是一样的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2015-08-25
    相关资源
    最近更新 更多