Is there any way to control views inside NavigationView header?

后端 未结 4 2265
我在风中等你
我在风中等你 2020-12-03 03:07

As the title says, I want to know if there is any way to control views inside the NavigationView header? (Except for adding or removing header.)

For exa

相关标签:
4条回答
  • 2020-12-03 03:26

    Since i can not accept a comment as an answer. So, i repost Moinkhan' answer here:

    first create header XML like lay_header.xml

    <TextView
        android:id="@+id/tvThought"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    on your java file inflate this above header in a TextView. like

    TextView headerView = (TextView) LayoutInflater.from(this).inflate(R.layout.lay_header, null);
    headerView.setText("Your_thoght");
    

    Now add it as a HeaderView

    navView = (NavigationView) findViewById(R.id.navView);
    navView.addHeaderView(headerView);
    

    Thats it...

    You can also check it out at: Customising NavigationView - Adding dynamic headerView, Android Support Design Library

    Many thanks! And again, we need a function that allow user to accept a comment as answer!

    0 讨论(0)
  • 2020-12-03 03:31

    Simply try find your view in your activity:

    tvUserName = (TextView) findViewById(R.id.tv_username);
    

    It works for me. My header layout is:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:background="?attr/colorPrimaryDark"
        android:padding="16dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        android:orientation="vertical"
        android:gravity="bottom">
    
        <TextView
            android:id="@+id/tv_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
    
    </LinearLayout>
    

    My NavigationView in Activity Layout is:

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>
    

    And BTW, I came across problem when inflating LinearLayout, where layout params are not provided:

    TextView headerView = (TextView) LayoutInflater.from(this).inflate(R.layout.lay_header, null);
    headerView.setText("Your_thoght");
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-03 03:40

    After updating your support library to version 23.1.1 or above,

    You could do this -

    View header = navigationView.getHeaderView(0);
    TextView text = (TextView) header.findViewById(R.id.textView);
    

    or if you have multiple headers

    navigationView.getHeaderCount()
    

    Ref : https://code.google.com/p/android/issues/detail?id=190226#c31

    0 讨论(0)
  • 2020-12-03 03:43

    The other way to make this is using the method "inflateHeaderView()" of NavigationView object, like below (After using "setContentView()"):

    View headerLayout = navigationView.inflateHeaderView(R.layout.yours_nav_header_layout);
    

    After this point you can use "headerLayout" to access custom views in your header layout, like below:

    View cutomView = (TextView) headerLayout.findViewById(R.id.lay_sign_in);
    

    In my case i'm using ButterKnife framework to inject View "navigationView" like this:

    @Bind(R.id.nav_view) NavigationView navigationView;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
        }
    
    0 讨论(0)
提交回复
热议问题