问题
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 example: In the header, I have a user avatar. By default, it displays a guest image, but after the user logs in, the real avatar will be showed.
How can this be accomplished?
回答1:
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
回答2:
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!
回答3:
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);
}
回答4:
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.
来源:https://stackoverflow.com/questions/31241683/is-there-any-way-to-control-views-inside-navigationview-header