Как найти представление в виде навигации по идентификатору из действия? - PullRequest
2 голосов
/ 27 апреля 2019

У меня есть макет активности, который вызывает другой XML-файл в качестве заголовка.Я хотел бы получить ссылку по идентификатору вида изображения, установленного в nav_header_main, но я не могу его достичь.

Activity_main:

<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">

  <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>

nav_header_main:

<LinearLayout 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="@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">

    <ImageButton
        android:background = "@drawable/roundedbuttonwhite"
        android:id="@+id/LogoButtonView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="fitCenter"
        android:paddingTop="15dp"
        android:src="@drawable/logoimg" />

</LinearLayout>

Обратите внимание, что nav_header_main вызывается в activity_layout.

Я пыталсяустановить OnClickListener в действии, но я получаю исключение NullException просто потому, что не могу добраться до этого представления ImageButton из действия.

logoImageButton = FindViewById<ImageButton>(Resource.Id.LogoButtonView); //Returns null
logoImageButton.SetOnClickListener(this);

Есть ли способ обойти это и получитьссылка?Похоже, я скучаю по простой вещи.

Ответы [ 2 ]

3 голосов
/ 27 апреля 2019

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);

0 голосов
/ 27 апреля 2019
nav_draw.getMenu().findItem(R.id.LogoButtonView).setVisible(false);
...