我正在尋找一種解決方案,以便如何在引用抽屜中設置用戶名和電子郵件的文本,從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 –