我的Header布局中有一个图像和一个文本,它通过android.support.design.widget.NavigationView包含在内。图片将是用户的个人资料图片,文字将是我从会话管理员班级获得的名称。
如何以编程方式访问和编辑文本和图像,因为它只包含在NavigationView的定义中。
以下是代码:
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/shitstuff"
app:itemTextColor="@color/grey"
app:menu="@menu/drawermenu"
app:headerLayout="@layout/headerlayout"
app:itemIconTint="@color/colorPrimary"
android:layout_marginTop="0dp"
/>
在活动课上:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new helpfragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_home) {
Intent i = new Intent(help.this, home.class);
startActivity(i);
}
if (menuItem.getItemId() == R.id.nav_item_tenant) {
Intent i = new Intent(help.this, rentertabview.class);
startActivity(i);
}
if (menuItem.getItemId() == R.id.nav_item_profile) {
Intent i = new Intent(help.this, profile.class);
startActivity(i);
}
if (menuItem.getItemId() == R.id.nav_item_owner) {
Intent i = new Intent(help.this, ownertabview.class);
startActivity(i);
}
if (menuItem.getItemId() == R.id.nav_item_services) {
Intent i = new Intent(help.this, services.class);
startActivity(i);
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
请详细说明。真的会感谢你。
答案 0 :(得分:0)
您只需使用
检索您的视图即可mNavigationView.findViewById(R.id.IDVIEWTOUPDATE)