Android NavigationView内部抽屉布局顶部的布局图像
我在抽屉布局中有一个NavigationView,NavigationView从屏幕左侧为抽屉提供布局。我使用它是为了向后兼容,我需要在顶部有一个图像,就像你通常在导航抽屉中看到的那样。我已经在WaveTavaVIEW中添加了一个IVIEVIEW,但是IVIEVIEW在我的抽屉里。我需要它在菜单项上方的顶部。这是我的示例代码Android NavigationView内部抽屉布局顶部的布局图像,android,android-layout,view,navigation-drawer,Android,Android Layout,View,Navigation Drawer,我在抽屉布局中有一个NavigationView,NavigationView从屏幕左侧为抽屉提供布局。我使用它是为了向后兼容,我需要在顶部有一个图像,就像你通常在导航抽屉中看到的那样。我已经在WaveTavaVIEW中添加了一个IVIEVIEW,但是IVIEVIEW在我的抽屉里。我需要它在菜单项上方的顶部。这是我的示例代码 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayou
<?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:aapp="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/image_background">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/colorPrimaryDark"
app:menu="@menu/menu_main"
app:itemTextColor="@color/text_field_font_color">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nav_header"
android:gravity="top"
android:src="@drawable/nav_header"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
我试着改变了android:Layou gravity和imageview的android:gravity,但没有成功。如何使此imageview在navigationView中的菜单项上方布局?您可以像这样从代码中动态充气
navigationView = (NavigationView) findViewById(R.id.NAV_ID_HERE);
View headerView = navigationView.inflateHeaderView(R.layout.nav_header);
在nav_header.xml中,您可以放置image
或任何您喜欢的内容
从主xml文件中删除您的
ImageView
,并将其放入nav_header.xml
只需右键单击项目->新建->活动->导航抽屉活动,您将看到一个足以理解如何实现它的示例
简而言之,添加app:headerLayout而不是
<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_main"
app:menu="@menu/activity_main_drawer"/>
您好,您可以这样给予
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/colorPrimaryDark"
app:menu="@menu/menu_main"
app:itemTextColor="@color/text_field_font_color"
app:headerLayout="@layout/nav_header_layout"
/>
layout/nav_header_layout.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="@dimen/nav_header_height"
android:background="@color/white"
android:gravity="bottom"
android:orientation="vertical"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/primaryTextColor"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
你可以像这样把它添加到你的导航布局中
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/colorPrimaryDark"
app:menu="@menu/menu_main"
app:itemTextColor="@color/text_field_font_color"
app:headerLayout="@layout/nav_header_layout"
/>
您可以创建
标题的另一个布局,并使用
属性app:headerLayout
1。根据需要使用ImageView
和其他小部件创建布局。在这里,我创建了带有ImageView
和两个TextView
的标题布局
nav\u header\u main.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="@dimen/nav_header_height"
android:orientation="vertical"
android:background="@color/colorAccent"
android:padding="16dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/nav_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textSize="18sp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio@android.com"
android:textSize="14sp"
android:textColor="#ffffff" />
</LinearLayout>
输出:
希望这会有帮助~您好,您可以在导航视图中放置布局并设置图像,然后它就可以工作了
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="?attr/colorPrimaryDark"
app:menu="@menu/menu_main"
app:itemTextColor="@color/text_field_font_color">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nav_header"
android:gravity="top"
android:src="@drawable/nav_header"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
这是为了使图像成为标题,但是现在标题在屏幕中间,上面和下面有一个巨大的空白空间。这太神奇了。它工作得很好。谢谢你的详细回答。