在android中如何在类文件中以编程方式设置导航抽屉标题图像和名称?

在Android Studio 1.4.1中,我创build了一个新的导航抽屉项目,这是默认的。我的问题是在这个项目中有导航标题图像和名称的nav_header_main.xml文件。 我希望这个图像和名称应该在我的主要类活动中以编程方式设置。 如何做到这一点,我尝试了很多,但应用程序崩溃。

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:id="@+id/headerView" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@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:paddingTop="@dimen/nav_header_vertical_spacing" android:src="@android:drawable/sym_def_app_icon" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android.studio@android.com" /> </LinearLayout> 

activity_main.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" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout> 

MainActivity.Class

 import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout headerImageView= (LinearLayout) findViewById(R.id.headerView); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); 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.setDrawerListener(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) { Toast.makeText(getApplicationContext(),"working",Toast.LENGTH_LONG).show(); return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camara) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } 
 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View hView = navigationView.getHeaderView(0); TextView nav_user = (TextView)hView.findViewById(R.id.nav_name); nav_user.setText(user); 

希望这个帮助!

正如在错误190226中提到的,从版本23.1.0获取页眉布局视图: navigationView.findViewById(R.id.navigation_header_text)不再有效。

解决方法是以编程方式膨胀headerview,并通过充气标题视图中的ID查找视图。

例如:

 View headerView = navigationView.inflateHeaderView(R.layout.navigation_header); headerView.findViewById(R.id.navigation_header_text); 

理想情况下,应该有一个方法getHeaderView()但已经提出,让我们看看,并等待它在devise支持库的function发布。

不要在xml中添加标题,通过膨胀布局来使用代码

 View hView = navigationView.inflateHeaderView(R.layout.nav_header_main); ImageView imgvw = (ImageView)hView.findViewById(R.id.imageView); TextView tv = (TextView)hView.findViewById(R.id.textview); imgvw .setImageResource(); tv.settext("new text"); 

这里是我的代码下面完美的工作不要在NavigationView标签在activity_main.xml中添加标题

 <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:menu="@menu/activity_main_drawer" app:itemBackground="@drawable/active_drawer_color" /> 

以下面的代码以编程方式添加标题

 View navHeaderView = navigationView.inflateHeaderView(R.layout.nav_header_main); headerUserName = (TextView) navHeaderView.findViewById(R.id.nav_header_username); headerMobileNo = (TextView) navHeaderView.findViewById(R.id.nav_header_mobile); headerMobileNo.setText("+918861899697"); headerUserName.setText("Anirudh R Huilgol"); 

我知道这是一个旧的职位,但我相信这可能会帮助一些人在路上。

你可以通过这样简单的获得Navigation视图的headerView元素:

  NavigationView mView = ( NavigationView ) findViewById( R.id.nav_view ); if( mView != null ){ LinearLayout mParent = ( LinearLayout ) mView.getHeaderView( 0 ); if( mParent != null ){ // Set your values to the image and text view by declaring and setting as you need to here. } } 

我希望这有助于某人。

这是旧的post,但对我来说是新的。 所以,这是直接的! 在这部分代码中:

  public boolean onNavigationItemSelected(MenuItem item) { 

},我将一个ImageView绑定到LinearLayout,它包含下面列出的示例中的ImageView。 注意:这与开始一个新项目时得到的代码相同,然后select模板“导航抽屉活动”:

 <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:src="@android:drawable/sym_def_app_icon" /> 

我在nav_header_main.xml中给出了LinearLayout和ID(在我的例子中,我select了'navigation_header_container',所以它是这样的:

 LinearLayout lV = (LinearLayout) findViewById(R.id.navigation_header_container); ivCloseDrawer = (ImageView) lV.findViewById(R.id.imageView); ivCloseDrawer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawer.closeDrawer(GravityCompat.START); } }); 

注意:在onCreate(MainActivity)之前,我有一个在顶部声明的私有ImageView ivCloseDrawer。

它工作正常! 希望它有帮助,最好的问候。

  nav = ( NavigationView ) findViewById( R.id.navigation ); if( nav != null ){ LinearLayout mParent = ( LinearLayout ) nav.getHeaderView( 0 ); if( mParent != null ){ // Set your values to the image and text view by declaring and setting as you need to here. SharedPreferences prefs = getSharedPreferences("user_data", MODE_PRIVATE); String photoUrl = prefs.getString("photo_url", null); String user_name = prefs.getString("name", "User"); if(photoUrl!=null) { Log.e("Photo Url: ", photoUrl); TextView userName = mParent.findViewById(R.id.user_name); userName.setText(user_name); ImageView user_imageView = mParent.findViewById(R.id.avatar); RequestOptions requestOptions = new RequestOptions(); requestOptions.placeholder(R.drawable.ic_user_24dp); requestOptions.error(R.drawable.ic_user_24dp); Glide.with(this).load(photoUrl) .apply(requestOptions).thumbnail(0.5f).into(user_imageView); } } } 

希望这可以帮助。

编辑:使用devise库高达23.0.1,但不适用于23.1.0

在主布局xml中,您将定义NavigationView ,使用app:headerLayout来设置标题视图。

 <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_drawer_header" app:menu="@menu/navigation_drawer_menu" /> 

@layout/nav_drawer_header将成为图片和文本的占位符。

nav_drawer_header.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="170dp" android:orientation="vertical"> <RelativeLayout android:id="@+id/headerRelativeLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/background" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/action_bar_size" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="#40000000" android:gravity="center" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingLeft="16dp" android:paddingRight="10dp" android:paddingTop="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="35dp" android:orientation="vertical" android:weightSum="2"> <TextView android:id="@+id/navHeaderTitle" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@android:color/white" /> <TextView android:id="@+id/navHeaderSubTitle" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@android:color/white" /> </LinearLayout> </LinearLayout> </RelativeLayout> </LinearLayout> 

而在你的主类中,你可以像正常的其他视图一样处理ImageviewTextView

 TextView navHeaderTitle = (TextView) findViewById(R.id.navHeaderTitle); navHeaderTitle.setText("Application Name"); TextView navHeaderSubTitle = (TextView) findViewById(R.id.navHeaderSubTitle); navHeaderSubTitle.setText("Application Caption"); 

希望这可以帮助。

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.addHeaderView(yourview);