【发布时间】:2019-09-16 17:37:43
【问题描述】:
我有一个活动布局,它调用另一个 xml 文件作为标题。我想通过nav_header_main 中设置的图像视图的 id 获得参考,但我无法访问它。
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);
有没有办法绕过它并获得参考?好像我错过了一件简单的事情。
【问题讨论】:
标签: java c# android xamarin findviewbyid