In android how to set navigation drawer header image and name programmatically in class file?

前端 未结 16 940
半阙折子戏
半阙折子戏 2020-12-04 07:53

In android studio 1.4.1, I have created new Navigation Drawer Project which is default.My issue is in this project there is nav_header_main.xml file which is for navigation

16条回答
  •  感情败类
    2020-12-04 08:25

    EDIT : Works with design library upto 23.0.1 but doesn't work on 23.1.0

    In main layout xml you will have NavigationView defined, in that use app:headerLayout to set the header view.

    
    

    And the @layout/nav_drawer_header will be the place holder of the image and texts.

    nav_drawer_header.xml

    
    
    
    
    
        
    
        
    
            
    
    
                
    
                
    
            
    
        
    
    
    

    And in your main class, you can take handle of Imageview and TextView as like normal other views.

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

    Hope this helps.

提交回复
热议问题