Java 如何在导航抽屉标题中更改 TextView 的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34973456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:11:45  来源:igfitidea点击:

How to change text of a TextView in navigation drawer header?

javaandroidtextview

提问by Salar Rastari

I want to change the text of a TextView inside the navigation drawer header. But I get this error:

我想更改导航抽屉标题内 TextView 的文本。但我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”



activity_main.xml

活动_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <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.v4.widget.DrawerLayout>

nav_header_main

nav_header_main

<?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="@drawable/slide_nav_bar"
    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">

    <ImageView
        android:id="@+id/tvHeaderIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/app_name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/tvHeaderName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

主活动.java

TextView tvHeaderName;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    tvHeaderName= (TextView) findViewById(R.id.tvHeaderName);
    tvHeaderName.setText("Saly");    
}

How can I do this?

我怎样才能做到这一点?

采纳答案by Bereket Gobeze

Use getHeaderView`on your navigationView

在您的导航视图上使用 getHeaderView`

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView navUsername = (TextView) headerView.findViewById(R.id.navUsername);
navUsername.setText("Your Text Here");

回答by Ashutosh Verma

You should inflate your Header layout from NavigationView first...

您应该首先从 NavigationView 膨胀您的 Header 布局...

navHeaderView= navigationView.inflateHeaderView(R.layout.nav_header_main);
tvHeaderName= (TextView) navHeaderView.findViewById(R.id.tvHeaderName);
tvHeaderName.setText("Saly"); 

回答by Neri

You can also use this as of 23.1.1 design support library

您也可以使用它作为 23.1.1 设计支持库

View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);

Reference: https://stackoverflow.com/a/33692431/2761923

参考:https: //stackoverflow.com/a/33692431/2761923

回答by Debasish Ghosh

What worked for me? its this:-

什么对我有用?它的:-

 View linearLayout=navigationView.inflateHeaderView(R.layout.nav_header_main);
      TextView name =  linearLayout.findViewById(R.id.welcomenametext);

But remember to remove this from your xml file i.e from NavigationView :-app:headerLayout="@layout/nav_header_main"

但请记住从您的 xml 文件中删除它,即从 NavigationView 中删除:-app:headerLayout="@layout/nav_header_main"



Ok apart from this above answer..over months i now figured there's a better way of doing it if you are advanced enough in android development!!Just put frame layout there and load fragment in it inside which we have Recyclerview and header section. With Recyclerview You can setup the header as you like with textviews,image,and many more as you like. The recent application I am working on just did that.Dynamically passed data into Recyclerview from fragmentactivity with out icons and also put a relative layout(inside textviews and imageview) which acts as header on top of Recyclerview.

好的,除了上面的答案..几个月来,我现在认为如果您在 android 开发方面足够先进,还有更好的方法!只需将框架布局放在那里并在其中加载片段,其中我们有 Recyclerview 和标题部分。使用 Recyclerview 您可以根据需要使用文本视图、图像等设置标题。我最近正在开发的应用程序就是这样做的。动态地将数据从 Fragmentactivity 传递到 Recyclerview 中,没有图标,并且还放置了一个相对布局(在 textviews 和 imageview 内),作为 Recyclerview 顶部的标题。

回答by Sachin Gupta

Use this to get the navigation header

使用它来获取导航标题

View hview = navigationView.getHeaderView(0);

View hview = navigationView.getHeaderView(0);

then use the hview to get the different views of navigation header and do whatever you want

然后使用 hview 获取导航标题的不同视图并执行您想要的任何操作

else using navHeaderView= navigationView.inflateHeaderView(R.layout.nav_header_main);this is creating a new header at index 1.

否则使用navHeaderView= navigationView.inflateHeaderView(R.layout.nav_header_main);它会在索引 1 处创建一个新标题。

回答by Gd Abhishek

Intially, we have to get the instance of navigation view by inflating the navigation view in a view varible, later you can get the textView instance to edit the textview

最初,我们必须通过在视图变量中膨胀导航视图来获取导航视图的实例,稍后您可以获取 textView 实例来编辑文本视图

回答by KNOWLEDGE GURU

  1. You need to get NavigationView id in your Activity like this

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView );
    
  2. Now get HeaderView Layout using Navigationview id like this

    View headerView = navigationView.getHeaderView(0);
    
  3. Now you can get Any view id and perform action what you want example

    TextView navUsername = (TextView) headerView.findViewById(R.id.navUsername);
    navUsername.setText("Your Text Here");
    
  1. 你需要像这样在你的 Activity 中获取 NavigationView id

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView );
    
  2. 现在使用 Navigationview id 获取 HeaderView 布局,如下所示

    View headerView = navigationView.getHeaderView(0);
    
  3. 现在您可以获取 Any view id 并执行您想要的操作示例

    TextView navUsername = (TextView) headerView.findViewById(R.id.navUsername);
    navUsername.setText("Your Text Here");
    

回答by Faisal Shaikh

This worked for me:

这对我有用:

NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView title = headerView.findViewById(R.id.nav_title);
TextView subTitle = headerView.findViewById(R.id.nav_sub_title);

回答by Siyabonga Dube

Here is the Kotlin version of the code, hope it helps someone out there.

这是代码的 Kotlin 版本,希望它可以帮助那里的人。

val navigationView : NavigationView  = findViewById(R.id.nav_view)
val headerView : View = navigationView.getHeaderView(0)
val navUsername : TextView = headerView.findViewById(R.id.txtUserName)
val navUserEmail : TextView = headerView.findViewById(R.id.txtEmail)

navUsername.text = username.toString()
navUserEmail.text = email.toString()

回答by Soumen Das

val navigationView = findViewById<View>(R.id.nav_view_home) as NavigationView
        val headerView = navigationView.getHeaderView(0)
        val tvUserName = headerView.findViewById<View>(R.id.tvUserName) as TextView
        tvUserName.text=(Preference.getInstance("your text")