NavigationDrawer 的主页导航 header 未在模拟器中显示
Home Navigation header of NavigationDrawer not showing in simulator
我已尽最大努力为 android 导航抽屉重现 UI 设计。
我已经将导航 header 设计为看起来像 UI 设计中的内容。
当我 运行 应用程序时,header(nav_header_home.xml 中的布局)在模拟器中不可见。
导航抽屉的nav_header_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/clientPrimaryBlue"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@drawable/home" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Home"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
activity_home_drawer.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">
<item android:title="Movies">
<menu>
<item
android:id="@+id/nav_action"
android:icon="@drawable/ic_menu_share"
android:title="Action" />
<item
android:id="@+id/nav_adventure"
android:icon="@drawable/ic_menu_send"
android:title="Adventure" />
<item
android:id="@+id/nav_comedy"
android:icon="@drawable/ic_menu_send"
android:title="Comedy" />
<item
android:id="@+id/nav_crime"
android:icon="@drawable/ic_menu_send"
android:title="Crime" />
<item
android:id="@+id/nav_drama"
android:icon="@drawable/ic_menu_send"
android:title="Drama" />
<item
android:id="@+id/nav_fantasy"
android:icon="@drawable/ic_menu_send"
android:title="Fantasy" />
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_actors"
android:icon="@drawable/ic_menu_camera"
android:title="Actors" />
<item
android:id="@+id/nav_directors"
android:icon="@drawable/ic_menu_gallery"
android:title="Directors" />
<item
android:id="@+id/nav_producers"
android:icon="@drawable/ic_menu_slideshow"
android:title="Producers" />
</group>
<item android:title="My Account">
<menu>
<item
android:id="@+id/nav_update_profile"
android:icon="@drawable/ic_menu_share"
android:title="Update Profile" />
<item
android:id="@+id/nav_reset_password"
android:icon="@drawable/ic_menu_send"
android:title="Reset Password" />
<item
android:id="@+id/nav_sign_out"
android:icon="@drawable/ic_menu_send"
android:title="Sign Out" />
</menu>
</item>
</menu>
预期菜单布局的屏幕截图
android 模拟器中的布局截图
app_bar_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="media.client.clientmediaandroid.activity.home.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_home" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
action_home.xml 与 DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<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/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
在@MikeM. 的帮助下,我能够以允许我看到导航的方式调整我的布局设置header。
他的贡献可以在这里直接阅读 -
Home Navigation header of NavigationDrawer not showing in simulator
按照
的建议,将 DrawerLayout
和 NavigationView
上的 fitsSystemWindows
属性设置为 false
我已尽最大努力为 android 导航抽屉重现 UI 设计。 我已经将导航 header 设计为看起来像 UI 设计中的内容。 当我 运行 应用程序时,header(nav_header_home.xml 中的布局)在模拟器中不可见。
导航抽屉的nav_header_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/clientPrimaryBlue"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@drawable/home" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Home"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
activity_home_drawer.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">
<item android:title="Movies">
<menu>
<item
android:id="@+id/nav_action"
android:icon="@drawable/ic_menu_share"
android:title="Action" />
<item
android:id="@+id/nav_adventure"
android:icon="@drawable/ic_menu_send"
android:title="Adventure" />
<item
android:id="@+id/nav_comedy"
android:icon="@drawable/ic_menu_send"
android:title="Comedy" />
<item
android:id="@+id/nav_crime"
android:icon="@drawable/ic_menu_send"
android:title="Crime" />
<item
android:id="@+id/nav_drama"
android:icon="@drawable/ic_menu_send"
android:title="Drama" />
<item
android:id="@+id/nav_fantasy"
android:icon="@drawable/ic_menu_send"
android:title="Fantasy" />
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_actors"
android:icon="@drawable/ic_menu_camera"
android:title="Actors" />
<item
android:id="@+id/nav_directors"
android:icon="@drawable/ic_menu_gallery"
android:title="Directors" />
<item
android:id="@+id/nav_producers"
android:icon="@drawable/ic_menu_slideshow"
android:title="Producers" />
</group>
<item android:title="My Account">
<menu>
<item
android:id="@+id/nav_update_profile"
android:icon="@drawable/ic_menu_share"
android:title="Update Profile" />
<item
android:id="@+id/nav_reset_password"
android:icon="@drawable/ic_menu_send"
android:title="Reset Password" />
<item
android:id="@+id/nav_sign_out"
android:icon="@drawable/ic_menu_send"
android:title="Sign Out" />
</menu>
</item>
</menu>
预期菜单布局的屏幕截图
android 模拟器中的布局截图
app_bar_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="media.client.clientmediaandroid.activity.home.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_home" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
action_home.xml 与 DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<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/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
在@MikeM. 的帮助下,我能够以允许我看到导航的方式调整我的布局设置header。
他的贡献可以在这里直接阅读 - Home Navigation header of NavigationDrawer not showing in simulator
按照
DrawerLayout
和 NavigationView
上的 fitsSystemWindows
属性设置为 false