登录后,我想更改name_user个人资料附近的文本视图中的文本。
但它不会在视觉上改变文本视图。
值得一提的是,在输出(Toast)时,它会给出所需的数据,但不直观地显示它。TextView 参数一切都很好(我认为),因为如果您在参数中设置完成的文本(我的意思是 android:text="smth"),它会直观地显示它。
爪哇代码:
`protected void onCreate(Bundle savedInstanceState) {
yourLayout = getLayoutInflater().inflate(R.layout.layout_navigation_header, null);
profileName = yourLayout.findViewById(R.id.profName); //
Intent intent = getIntent(); // Get data from previous activity.
String name_user = intent.getStringExtra("name");
String email_user = intent.getStringExtra("email");
String password_user = intent.getStringExtra("password");
profileName.setText(name_user); //0 changes, textView still don't change.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
Toast toast = Toast.makeText(getApplicationContext(), profileName.getText().toString(),
Toast.LENGTH_SHORT); // for debug, it works and show profileName that contains name_user, but.
toast.show();
findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
}`
主 XML 的一部分
`<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/layout_navigation_header"// layour_navigation_header -here is TextView
app:menu='@menu/navigation_menu'
android:layout_gravity="start"/>`
我需要更改的 TextView 的部分layour_navigation_header。
`<TextView
android:id="@+id/profName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Temporary"
app:layout_constraintBottom_toTopOf="@id/viewSupporter"
app:layout_constraintStart_toEndOf="@id/imageProfile"/>`
希望你能帮助我
我试图移动
`yourLayout = getLayoutInflater().inflate(R.layout.layout_navigation_header, null);
profileName = yourLayout.findViewById(R.id.profName); //
Intent intent = getIntent(); // Get data from previous activity.
String name_user = intent.getStringExtra("name");
String email_user = intent.getStringExtra("email");
String password_user = intent.getStringExtra("password");
profileName.setText(name_user); //0 changes, textView still don't change`
以前
`super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);`
但最终结果保持不变。它包含数据,但不直观地显示它。
您正在膨胀layout_navigation_header
布局并在其文本视图中设置值。但是您似乎从未将布局放在屏幕上,布局实例只是被丢弃。
显示的是您使用setContentView()
膨胀并设置为内容视图的activity_menu2
布局。如果该布局包括layout_navigation_header
或其具有某种机制的外观,则它与您之前膨胀的实例不同。
要解决此问题,只需调用setContentView()
设置所需的布局,调用findViewById()
以查找文本视图并为其设置文本。