首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据活动中的ID在导航视图中查找视图?

如何根据活动中的ID在导航视图中查找视图?
EN

Stack Overflow用户
提问于 2019-04-27 20:33:50
回答 2查看 394关注 0票数 1

我有一个activity布局,它调用另一个xml文件作为头文件。我想通过nav_header_main中设置的图像视图的id来获取引用,但我无法访问它。

activity_main:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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中调用的。

我试图在activity中设置一个OnClickListener,但得到了一个NullException,原因很简单,因为我无法从activity到达那个ImageButton视图。

代码语言:javascript
复制
logoImageButton = FindViewById<ImageButton>(Resource.Id.LogoButtonView); //Returns null
logoImageButton.SetOnClickListener(this);

有没有办法绕过它并获得参考资料?看起来我错过了一件简单的事情。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55880599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档