在android中,如何在类文件中以编程方式设置导航抽屉标题图像和名称?

在android中,如何在类文件中以编程方式设置导航抽屉标题图像和名称?,android,android-layout,android-fragments,androiddesignsupport,android-navigationview,Android,Android Layout,Android Fragments,Androiddesignsupport,Android Navigationview,在android studio 1.4.1中,我创建了新的默认导航抽屉项目。我的问题是在这个项目中有nav_header_main.xml文件,用于导航标题图像和名称。我希望这个图像和名称应该在我的主类活动中以编程方式设置。如何做到这一点,我尝试了很多,但应用程序崩溃 nav_header_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="mat

在android studio 1.4.1中,我创建了新的默认导航抽屉项目。我的问题是在这个项目中有nav_header_main.xml文件,用于导航标题图像和名称。我希望这个图像和名称应该在我的主类活动中以编程方式设置。如何做到这一点,我尝试了很多,但应用程序崩溃

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout        
android:layout_width="match_parent"
android:id="@+id/headerView"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_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/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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="Android Studio"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="android.studio@android.com" />

</LinearLayout>
val navigationView: NavigationView =  findViewById(R.id.nv)
val header: View = navigationView.getHeaderView(0)
val tv: TextView = header.findViewById(R.id.profilename)
tv.text = "Your_Text"

编辑:适用于高达23.0.1版本的设计库,但不适用于23.1.0版本

在主布局xml中,您将定义
NavigationView
,使用
app:headerLayout
设置标题视图

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_drawer_header"
        app:menu="@menu/navigation_drawer_menu" />
在主类中,您可以像处理普通其他视图一样处理
Imageview
TextView

TextView navHeaderTitle = (TextView) findViewById(R.id.navHeaderTitle);
navHeaderTitle.setText("Application Name");

TextView navHeaderSubTitle = (TextView) findViewById(R.id.navHeaderSubTitle);
navHeaderSubTitle.setText("Application Caption");

希望这能有所帮助。

不要在xml中添加标题,使用膨胀布局的代码添加

View hView =  navigationView.inflateHeaderView(R.layout.nav_header_main);
ImageView imgvw = (ImageView)hView.findViewById(R.id.imageView);
TextView tv = (TextView)hView.findViewById(R.id.textview);
imgvw .setImageResource();
tv.settext("new text");
如错误中所述,自版本23.1.0获得标题布局视图以来:
navigationView.findViewById(R.id.navigation\u header\u text)
不再工作

解决方法是通过编程方式对headerview进行充气,并从充气的header视图中按ID查找视图

例如:

View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
headerView.findViewById(R.id.navigation_header_text);
理想情况下应该有一个方法
getHeaderView()
,但它已经被提出了,让我们看看并等待它在设计支持库的功能发布中发布

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View hView =  navigationView.getHeaderView(0);
TextView nav_user = (TextView)hView.findViewById(R.id.nav_name);
nav_user.setText(user);

希望这有帮助

NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.addHeaderView(yourview)

这是我的代码 下面是完美的工作 不要在NavigationView中添加标题 活动_main.xml中的标记


我知道这是一个老帖子,但我相信这可能会帮助一些人的道路

通过执行以下操作,您只需获取导航视图的headerView元素:

 NavigationView mView = ( NavigationView ) findViewById( R.id.nav_view );

 if( mView != null ){
     LinearLayout mParent = ( LinearLayout ) mView.getHeaderView( 0 );

     if( mParent != null ){
        // Set your values to the image and text view by declaring and setting as you need to here. 
     }
 }

我希望这对某人有所帮助。

这是一篇旧文章,但对我来说是新文章。所以,这是直截了当的! 在守则的这部分:

 public boolean onNavigationItemSelected(MenuItem item) {
} ,我将ImageView绑定到LinearLayout,其中包含下面列出的示例中的ImageView。注意:这与您启动新项目并选择“导航抽屉活动”模板时得到的代码相同:

注意:在onCreate(MainActivity)之前,我在顶部声明了一个私有ImageView ivCloseDrawer

它工作得很好! 希望有帮助, 致以最良好的祝愿

  nav = ( NavigationView ) findViewById( R.id.navigation );

    if( nav != null ){
        LinearLayout mParent = ( LinearLayout ) nav.getHeaderView( 0 );

        if( mParent != null ){
            // Set your values to the image and text view by declaring and setting as you need to here.

            SharedPreferences prefs = getSharedPreferences("user_data", MODE_PRIVATE);
            String photoUrl = prefs.getString("photo_url", null);
            String user_name = prefs.getString("name", "User");

            if(photoUrl!=null) {
                Log.e("Photo Url: ", photoUrl);

                TextView userName = mParent.findViewById(R.id.user_name);
                userName.setText(user_name);

                ImageView user_imageView = mParent.findViewById(R.id.avatar);

                RequestOptions requestOptions = new RequestOptions();
                requestOptions.placeholder(R.drawable.ic_user_24dp);
                requestOptions.error(R.drawable.ic_user_24dp);

                Glide.with(this).load(photoUrl)
                        .apply(requestOptions).thumbnail(0.5f).into(user_imageView);

            }

        }
    }

希望这有帮助。

首先,您需要访问MainActivity(或calling activity)中的导航抽屉,如下所示:

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
<?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">

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

<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:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout>
然后需要从activity_main.xml中删除标题布局,因为布局将在MainActivity中以编程方式膨胀。您的activity_main.xml应如下所示:

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
<?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">

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

<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:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout>
您可以在Kotlin中阅读更多内容

    val hView = nav_view.getHeaderView(0)
    val textViewName = hView.findViewById(R.id.textViewName) as TextView
    val textViewEmail = hView.findViewById(R.id.textViewEmail) as TextView
    val imgvw = hView.findViewById(R.id.imageView) as ImageView
    imgvw.setImageResource(R.drawable.ic_menu_gallery)

还可以使用Kotlinx功能

val hView = nav_view.getHeaderView(0)
hView.textViewName.text = "lorem ipsum"
hView.imageView.setImageResource(R.drawable.ic_menu_gallery)
FirebaseAuth FirebaseAuth=FirebaseAuth.getInstance();

NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图)//显示导航抽屉标题的文本。 View headerview=navigationView.getHeaderView(0); TextView tt1=(TextView)headerview.findViewById(R.id.TextView\u用户名); tt1.setText(firebaseauth.getCurrentUser().getDisplayName())//登录用户的用户名。 TextView tt=(TextView)headerview.findViewById(R.id.TextView\u emailid); tt.setText(firebaseauth.getCurrentUser().getEmail())//已登录用户的电子邮件id。 最终ImageView img1=(ImageView)headerview.findViewById(R.id.ImageView\u userimage); Glide.with(getApplicationContext()) .load(firebaseauth.getCurrentUser().getPhotoUrl()).asBitmap().atMost().error(R.drawable.ic_selfie_point_icon)//加载后始终显示asBitmap。 .into(新的SimpleTarget(){ @凌驾 public void onResourceReady(位图资源、动画)
这将解决您的问题以下是您可以用来获取标题视图和设置数据的方法

val headerView: View? = navigationView.getHeaderView(0) // Index of the added headerView  

// Now you can access child views of the header view
val titleTextView: TextView? = headerView?.findViewById(R.id.titleTextView)

如果您使用的是绑定,则可以执行以下操作

val headerView = binding.navView.getHeaderView(0)
val headerBinding = NavDrawerHeaderBinding.bind(headerView)
headerBinding.textView.text = "Your text here"

我已经实现了它,而且它运行没有任何问题。我使用这种方式已经很长时间了。我在设计库22.2.1中使用了它,它运行良好。从未在23.1.0上尝试过。我尝试过,但应用程序崩溃。navHeaderTitle.setText(“应用程序名称”)附近显示日志cat错误您是否正在使用支持库23.1.0?如果是这样,请查看@PareshMayani在上述评论中提供的链接。请说明如何添加文本视图和图像视图,如回答中所述。您可以使用
充气头视图()充气头布局(使用图像视图、文本视图或您想要在头布局中的任何视图)
方法,可以使用
findViewById()
NavigationView NavigationView=null;view headerView=NavigationView.inflateHeaderView(R.layout.nav\u header\u main);TextView tv=(TextView)headerView.findViewById(R.id.TextView);tv.setText(“检查”);这是我的代码,即使应用程序无法使用给定的示例,请记住从
android.support.design.widget.NavigationView
app:headerLayout=“@layout/nav_header\u main”中删除行
因为您已经充气了programmatically@milapTank注释有帮助,因为标题视图膨胀了两次,直到我从我的XML中删除了他建议的行。您好,欢迎使用Stack Overflow。感谢您的贡献。您知道这个问题已经存在4个多月了吗:-)?不确定是否仍然需要答案…但是,对于不同的答案,尝试多解释一点。你的建议中的
yourview
是什么,OP应该把你的代码放在哪里?她/他应该添加行还是用你的行替换相同的行?这样的解释将有助于尝试你的答案,并看到你的贡献的价值但是,祝你好运。值得一提的是,这个问题没有明确的答案,它仍然是活跃的:你是否意识到,由于这个任意的4个月期限已经过去了,有
//inflate header layout
View navView =  navigationView.inflateHeaderView(R.layout.nav_header_main);
//reference to views
ImageView imgvw = (ImageView)navView.findViewById(R.id.imageView);
TextView tv = (TextView)navView.findViewById(R.id.textview);
//set views
imgvw.setImageResource(R.drawable.your_image);
tv.setText("new text");

navigationView.setNavigationItemSelectedListener(this);
    val hView = nav_view.getHeaderView(0)
    val textViewName = hView.findViewById(R.id.textViewName) as TextView
    val textViewEmail = hView.findViewById(R.id.textViewEmail) as TextView
    val imgvw = hView.findViewById(R.id.imageView) as ImageView
    imgvw.setImageResource(R.drawable.ic_menu_gallery)
val hView = nav_view.getHeaderView(0)
hView.textViewName.text = "lorem ipsum"
hView.imageView.setImageResource(R.drawable.ic_menu_gallery)
   FirebaseAuth firebaseauth = FirebaseAuth.getInstance(); 

   NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);   //displays text of header of nav drawer.
    View headerview = navigationView.getHeaderView(0);

    TextView tt1 = (TextView) headerview.findViewById(R.id.textview_username);
    tt1.setText(firebaseauth.getCurrentUser().getDisplayName());//username of logged in user.  

   TextView tt = (TextView) headerview.findViewById(R.id.textView_emailid);
    tt.setText(firebaseauth.getCurrentUser().getEmail());    //email id of logged in user.

    final ImageView img1 = (ImageView) headerview.findViewById(R.id.imageView_userimage);
    Glide.with(getApplicationContext())
            .load(firebaseauth.getCurrentUser().getPhotoUrl()).asBitmap().atMost().error(R.drawable.ic_selfie_point_icon)   //asbitmap after load always.
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    img1.setImageBitmap(resource);
                }
            });
val navigationView: NavigationView =  findViewById(R.id.nv)
val header: View = navigationView.getHeaderView(0)
val tv: TextView = header.findViewById(R.id.profilename)
tv.text = "Your_Text"
val headerView: View? = navigationView.getHeaderView(0) // Index of the added headerView  

// Now you can access child views of the header view
val titleTextView: TextView? = headerView?.findViewById(R.id.titleTextView)
val headerView = binding.navView.getHeaderView(0)
val headerBinding = NavDrawerHeaderBinding.bind(headerView)
headerBinding.textView.text = "Your text here"