更改NavigationView的标题标题

时间:2015-10-25 18:57:07

标签: android android-layout navigation-drawer

我在NavigationView内嵌套DrawerLayout

enter image description here

activity_main.xml中

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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="match_parent"
android:fitsSystemWindows="true">

.......

<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/drawer_header"
    app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

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="150dp"
              android:background="?attr/colorPrimaryDark"
              android:gravity="bottom"
              android:orientation="vertical"
              android:padding="16dp"
              android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <TextView
        android:id="@+id/drawerHeaderTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/drawer_header_text"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>

如何在Java代码中访问抽屉标题的标题? 我试着:

  1. 使用id drawerHeaderTitle )直接访问它,但我得到NullPointerException

  2. 通过NavigationView.findViewById:相同的错误

  3. 访问它

    我该如何克服这个问题?

5 个答案:

答案 0 :(得分:3)

如果使用'com.android.support:design:23.1.1'进行编译,则可以通过以下方式访问标题:

private NavigationView mNavigation;
private View mHeaderView;

private TextView mDrawerHeaderTitle;

mNavigation = (NavigationView) findViewById(R.id.navigation_view);
mHeaderView = mNavigation.getHeaderView(0);

mDrawerHeaderTitle = (TextView) mHeaderView.findViewById(R.id.drawerHeaderTitle);

有关此内容的更多信息:http://developer.android.com/intl/es/tools/support-library/index.html

答案 1 :(得分:2)

使用这种方法

 mNavigationView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {

                mNavigationView.removeOnLayoutChangeListener( this );

                TextView textView = (TextView) mNavigationView.findViewById(R.id.drawerHeaderTitle);

                textView.setText("title");
            }
        });

答案 2 :(得分:1)

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    View mHeaderView = navigationView.getHeaderView(0);
    TextView companyNameTxt = (TextView) mHeaderView.findViewById(R.id.nav_company_name);
    companyNameTxt.setText("Test Company Name, LLC");

答案 3 :(得分:1)

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="Dashboard"/>

app:title =“Dashboard” - &gt;你可以在这里改变你的头衔..

答案 4 :(得分:0)

Declaration 
private NavigationView mNavigationView;
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
 mNavigationView = (NavigationView) findViewById(R.id.nav_view);
 mTextView = (TextView) mNavigationView.getHeaderView(0).findViewById(R.id.id_textView);
 mTextView.setText("text user");
}
相关问题