Тож у мене є діяльність, яка виглядає приблизно так:
У перегляді навігації є текстове перегляд, який передбачає показати ім'я користувача під час входу користувача. Але я не знаю, як це зробити ...
Ось xml, який я використовую (activity_home.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"
android:id="@+id/drawer_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.epizy.someone.tetrisunda.home">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<ImageButton
android:layout_marginRight="10dp"
android:background="?attr/selectableItemBackground"
android:layout_gravity="right"
android:src="@drawable/ic_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<include layout="@layout/content_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_home"
android:fitsSystemWindows="true"
android:layout_gravity="start"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
А перегляд тексту знаходиться у файлі XML nav_header, це "виглядає приблизно так:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="#eeeeeeee"
android:layout_width="match_parent"
android:layout_height="192dp">
<TextView
android:id="@+id/menu_home_username"
android:layout_gravity="bottom"
android:textColor="@color/colorPrimary"
android:text="USERNAME"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
Як я можу змінити текст у nav_header за допомогою home.java?
Відповіді:
0 для відповіді № 1Ви змінюєте текст у nav_header, подібний до цього, з домашньої сторінки.java -
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
menu_home_username = (TextView)header.findViewById(R.id.menu_home_username);
menu_home_username.setText("set any text here");
0 для відповіді № 2
Ласкаво проведіть ці рядки.
TextView menu_home_username= (TextView)findViewById(R.id.menu_home_username);
menu_home_username.setText("SomeText");