我正在寻找一种解决方案,以便如何在引用抽屉中设置用户名和电子邮件的文本,从MainActivity引用其他布局中的ID。在MainActivity中使用它的布局在导航抽屉中显示登录的用户
nav_header_main.xml
下面的代码显示所登录的用户从activity_main.xml中引用TextViews但我需要的 “R.id.”以引用nav_header_main.xml中的用户名和电子邮件TextViews,以便能够在导航抽屉标题中显示用户的详细信息。
MainActivity.java
if(!SharedPrefManager.getInstance(this).isLoggedIn()){
finish();
startActivity(new Intent(this, LoginActivity.class));
}
textviewUsername = (TextView)findViewById(R.id.username);
textviewEmail = (TextView)findViewById(R.id.email);
/*
textviewUsername = (TextView)findViewById(R.id.textViewUsernameNav);
textViewEmail= (TextView)findViewById(R.id.textViewEmailNav); */
textviewEmail.setText(SharedPrefManager.getInstance(this).getEmail());
textviewUsername.setText(SharedPrefManager.getInstance(this).getUsername());
nav_header_xml
android:id="@+id/nav_header_main_id"
<TextView
android:id="@+id/textViewUsernameNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Username"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textViewEmailNav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[email protected]" />
添加导航视图布局和主要活动的XML –
试试我的答案更新导航headerview用户名和电子邮件的TextView – FAT
问题是不是让我引用nav_header_main.xml里面的TextViews –