gpt4 book ai didi

android - 在 DrawerLayout 内的 NavigationView 顶部布局图像

转载 作者:行者123 更新时间:2023-11-29 19:12:21 25 4
gpt4 key购买 nike

我在 DrawerLayout 中有一个 NavigationView,NavigationView 为来自屏幕左侧的抽屉提供布局。我使用它是为了向后兼容,我需要在顶部有一个图像,就像通常在抽屉导航中那样。我已将 ImageView 添加到 NavigatonView,但 ImageView 位于我的抽屉中间。我需要在菜单项上方的顶部。这是我的示例代码...

<?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:layout_gravity 和 imageview 的 android:gravity,但没有成功。我怎样才能让这个 imageview 布局在 navigationView 中的菜单项之上?

最佳答案

Instead of adding ImageView inside NavigationView, you can create another layout for header and set it to NavigationView using attribute app:headerLayout.

1. 根据您的需要使用 ImageView 和其他小部件创建布局。在这里,我使用 ImageView 和两个 TextView 创建了页眉布局。

nav_header_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>

enter image description here

2. 使用属性 app:headerLayout="@layout/nav_header_main"< 将布局 nav_header_main 添加到 NavigationView 作为页眉布局.

<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_main">

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

输出:

enter image description here

希望对你有帮助~

关于android - 在 DrawerLayout 内的 NavigationView 顶部布局图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com