gpt4 book ai didi

android - 抽屉导航标题是否超过了内容?

转载 作者:行者123 更新时间:2023-11-29 15:39:55 25 4
gpt4 key购买 nike

我正在尝试在我的抽屉导航中添加标题。我是 android studio 的初学者,所以任何提示都会对我有很大帮助

问题是这个标题在我的抽屉导航项目上,所以我看不到我的一些抽屉导航项目。

如何正确添加此 header ?另外,我的 textview 内容没有显示,为什么?

<?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"
tools:context="com.sk.mf.UserAreaActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:id="@+id/drawerLayout">


<FrameLayout
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/content_frame">
</FrameLayout>


<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@layout/navigation_menu"
android:layout_gravity="start">



<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorPrimary">

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/nh_User"
android:textSize="24sp"
android:textColor="#ffffff" />

</RelativeLayout>






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



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

最佳答案

NavigationView 的 header 标签中创建 Header:

 <android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white"
app:headerLayout="@layout/nav_header" //layout of Header
app:itemIconTint="@color/colorPrimary"
app:menu="@menu/menu_navigation" />

在您的 layout/nav_header.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="190dp"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:scaleType="fitXY"
android:src="@drawable/drawer_header" />
</LinearLayout>

要访问页眉布局内容,请使用:

 NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0); //get the header view
TextView text = (TextView) header.findViewById(R.id.nh_User);
text.setText("Hello");

关于android - 抽屉导航标题是否超过了内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467127/

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