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

标签 android android-studio textview android-xml

我正在尝试在我的抽屉导航中添加标题。 我是 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/

相关文章:

android - AppCompatTextView autoSizeTextType 不起作用

android - 带有权重的 TextView 无法水平居中文本

android - 使用 LoopJ Android syncHttp 从 https 下载图片资源

android - 可扩展通知android 4.1

android-studio - Windows 10 中的 adb.exe 位于哪里?

android - 在自定义通知布局中更新 TextView

android - 亚马逊 Kindle 设备的 map 导航 Intent

android - Canvas 上的鼠标单击(或触摸)事件导致使用 HTML5、Phonegap 和 Android 进行选择

java - Android,如何在项目中包含不是 JAR 文件的特定库?

android - 渲染问题 @id/visible Android Studio