Android:顶部 NavigationView 内容消失了

Android: Top NavigationView content gone

我将此代码用于导航抽屉

<android.support.v4.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:openDrawer="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            android:visibility="visible">
            <include
                layout="@layout/menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </android.support.design.widget.NavigationView>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="ir.app.ir.live24.MainActivity"
    android:clickable="false"
    android:gravity="center"
    android:background="#fff">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView3"
            android:textColor="#000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView4"
            android:textColor="#000"
            android:layout_weight="0.5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView5"
            android:textColor="#000" />
    </LinearLayout>
</RelativeLayout>

鉴于 android 工作室工作正确: http://i65.tinypic.com/wtvu4k.jpg

但是在启动后的设备中,菜单顶部消失了: http://i68.tinypic.com/2nu0ub5.jpg

下一个问题:可点击菜单不起作用

请帮帮我

您不应添加菜单布局:在 navigationView 中,您可以直接添加菜单..使用 app:menu="@menu/menu_navigation"

使用此布局:

 <android.support.v4.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:openDrawer="start">


    <include
        layout="@layout/menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        app:menu="@menu/menu_navigation"   //menu items in drawer
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:visibility="visible"></android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

现在 res/menu/menu_navigation 添加如下菜单项:

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/profile"
            android:icon="@drawable/profile_icon"
            android:title="Profile" />

        <item
            android:id="@+id/settings"
            android:icon="@drawable/settings_icon"
            android:title="Settings" />

        <item
            android:id="@+id/about_us"
            android:icon="@drawable/about_us"
            android:title="About us" />

    </group>
</menu>

这是完整的Example

# 问题 1: 菜单顶部不见了。

1. 添加 android:paddingTop="24dp"menu 容器 RelativeLayout 以显示 top 菜单 TextView

2.TextView中添加android:background="?attr/selectableItemBackground",当click时显示ripple effect

更新您的 menu.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:clickable="false"
    android:gravity="center"
    android:paddingTop="24dp"
    android:background="#fff">

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

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="New Text"
            android:textColor="#000"
            android:background="?attr/selectableItemBackground"/>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="New Text"
            android:textColor="#000"
            android:layout_weight="0.5" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="New Text"
            android:textColor="#000"
            android:background="?attr/selectableItemBackground"/>
    </LinearLayout>
</RelativeLayout>

仅供参考,我已将 android:padding="16dp" 添加到所有 TextView 以提供适当的缩进。

# 问题 2:可点击菜单无效。

onClick 侦听器设置为 TextView 以处理 click 事件:

// From Activity onCreate() method

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ........................
    ...............................

    TextView textView3 = (TextView) findViewById(R.id.textView3);

    textView3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

输出:

希望对你有所帮助~