我想将标头的名称和图像更改为当前登录人员的用户名,但不知道该怎么做。我是android studio的新手,也不知道它是如何工作的,我也试图找到 @string/nav_header_subtitle,我在任何地方都找不到它,有人可以帮助我吗?
编辑:
这是主要活动
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener();
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_profile) {
} else if (id == R.id.nav_balance) {
} else if (id == R.id.nav_setting) {
} else if (id == R.id.nav_help) {
} else if (id == R.id.nav_logout) {
final ProgressDialog dlg = new ProgressDialog(MainActivity.this);
ParseUser.logOut();
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我应该将代码放在哪里?
编辑:我做了下面的代码和this showed up
编辑: 标题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/txt_namedisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />
答案 0 :(得分:0)
首先在NavigationView标头中定义TextView和ImageView 然后在您的Java代码中完成
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View HeaderView= navigationView.getHeaderView(0);
TextView TextHeaderTitle = (TextView) headerView.findViewById(R.id.txt_namedisplay);
TextView TextHeaderSubTitle = (TextView) headerView.findViewById(R.id.txt_namedisplay);
ImageView ImageUser = (ImageView) headerView.findViewById(R.id.imageView);
TextHeaderTitle.setText("TITLE TEXT");
TextHeaderSubTitle .setText("SUB TITLE TEXT");
ImageHeaderNav.setImageResource(R.drawable.image_user);
答案 1 :(得分:0)
解决方案:
您可以像这样从HeaderView
对象中获取NavigationView
:
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0);
然后
TextView HeaderName = (TextView) header.findViewById(R.id.txt_namedisplay);
TextView HeaderEmail = (TextView) header.findViewById(R.id.textView);
ImageView imgHeaderLogo = (ImageView) header.findViewById(R.id.imageView);
然后,获取要显示的字符串:
String title = "Your_Title_String";
String desc = "Your_Description_String";
String image = "Your_base64_Image";
HeaderName.setText(title);
HeaderEmail.setText(desc);
然后,对于配置文件映像(如果您具有BASE64字符串),请使用:
byte[] image = Base64.decode(image, Base64.DEFAULT);
int i = image.length;
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
imgHeaderLogo.setImageBitmap(bmp);
在onCreate()
就是这样。
希望有帮助。
答案 2 :(得分:0)
将您的主要活动更改为以下内容:
negative